-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.java
More file actions
166 lines (146 loc) · 3.92 KB
/
Copy pathMyArray.java
File metadata and controls
166 lines (146 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Iterator;
public class MyArray<T> implements Iterable<T> {
private T[] array;
private int size;
private int capacity;
public MyArray(int capacity){
if(capacity <= 0){
System.out.print("Error: capacity <= 0.");
}
this.array = (T[]) new Object[capacity];
this.capacity = capacity;
size = 0;
}
public void buildArray(T[] arr){
for(T ele: arr){
insert(ele);
}
}
public int getSize() {
return size;
}
public int getCapacity() {
return capacity;
}
public void insert(T data){
if(size < array.length){
array[size] = data;
size++;
}else if(size == capacity){
capacity *= 2;
T[] newArray = (T[]) new Object[capacity];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
array[size] = data;
size++;
}else{
System.out.println("error: length > capacity.");
}
}
public boolean insert(int index, T data){
if(index >= size){
return false;
}
if(size < capacity){
for(int i = size - 1; i >= index; i--){
array[i+1] = array[i];
}
array[index] = data;
size++;
return true;
}else if(size == capacity){
capacity *= 2;
T[] newArray = (T[]) new Object[capacity];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
for(int i = size - 1; i >= index; i--){
array[i+1] = array[i];
}
array[index] = data;
size++;
return true;
}else{
System.out.println("error: length > capacity.");
return false;
}
}
public boolean delete(int index){
if(index >= size){
return false;
}
for(int i = index; i < size - 1; i++){
array[i] = array[i+1];
}
size--;
return true;
}
public boolean delete(){
if(size == 0){
return false;
}
size--;
return true;
}
public T getData(int index){
if(index >= size || index < 0){
System.out.println("Error: index is < 0 or >= size");
return null;
}
return array[index];
}
public boolean update(int index, T data){
if(index >= size || index < 0){
System.out.println("Error: index is < 0 or >= size");
return false;
}
array[index] = data;
return true;
}
public int searchArray(T data){
for(int i=0; i<size; i++){
if(data.equals(array[i])){
return i;
}
}
return -1;
}
public String printArray(){
String out = "";
for(int i = 0; i< size; i++){
out += array[i] + ", ";
}
out += '\n';
return out;
}
public static void main(String[] args) {
MyArray<Integer> array = new MyArray<Integer>(3);
array.insert(1);
array.insert(2);
array.insert(1,3);
array.insert(1,4);
array.delete();
array.insert(99);
System.out.print(array.printArray());
array.insert(4);
array.delete(1);
array.insert(5);
array.insert(6);
array.insert(7);
array.insert(8);
array.insert(9);
System.out.print(array.printArray());
for(Integer arr: array){
System.out.print(arr);
}
}
@Override
public Iterator<T> iterator() {
T[] realArray = (T[])new Object[size];
for(int i=0; i<size; i++){
realArray[i] = array[i];
}
return Arrays.stream(realArray).iterator();
}
}