-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyArray.java
More file actions
131 lines (109 loc) · 3.41 KB
/
MyArray.java
File metadata and controls
131 lines (109 loc) · 3.41 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
package me.smartstore.arrays;
import me.smartstore.arrays.exception.ElementNotFoundException;
import me.smartstore.arrays.exception.EmptyArrayException;
import me.smartstore.arrays.exception.NullArgumentException;
public class MyArray<T> implements Collections<T> {
protected static final int DEFAULT = 10;
protected T[] arrays;
protected int size;
protected int capacity;
public MyArray() {
arrays = (T[]) new Object[DEFAULT];
capacity = DEFAULT;
}
public MyArray(int initial) {
arrays = (T[]) new Object[initial];
capacity = initial;
}
public MyArray(T[] arrays) {
this.arrays = arrays;
capacity = arrays.length;
size = arrays.length;
}
@Override
public int size() {
return size;
}
protected int capacity() {
return capacity;
}
@Override
public T get(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
return arrays[index];
}
@Override
public void set(int index, T object) throws IndexOutOfBoundsException, NullArgumentException {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
if (object == null) throw new NullArgumentException();
arrays[index] = object;
}
@Override
public int indexOf(T object) throws NullArgumentException, ElementNotFoundException {
if (object == null) throw new NullArgumentException();
for (int i = 0; i < size; i++) {
if (arrays[i] == null) continue;
if (arrays[i].equals(object)) return i;
}
throw new ElementNotFoundException();
}
@Override
public void add(T object) throws NullArgumentException {
if (object == null) throw new NullArgumentException();
if (size < capacity) {
arrays[size] = object;
size++;
} else {
grow();
add(object);
}
}
@Override
public void add(int index, T object) throws IndexOutOfBoundsException, NullArgumentException {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
if (object == null) throw new NullArgumentException();
if (size < capacity) {
for (int i = size-1; i >= index ; i--) {
arrays[i+1] = arrays[i];
}
arrays[index] = object;
size++;
} else {
grow();
add(index, object);
}
}
@Override
public T pop() {
return pop(size-1);
}
@Override
public T pop(int index) throws IndexOutOfBoundsException {
if (size == 0) throw new EmptyArrayException();
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
T popElement = arrays[index];
arrays[index] = null;
for (int i = index+1; i < size; i++) {
arrays[i-1] = arrays[i];
}
arrays[size-1] = null;
size--;
return popElement;
}
@Override
public T pop(T object) {
return pop(indexOf(object));
}
protected void grow() {
capacity *= 2;
arrays = java.util.Arrays.copyOf(arrays, capacity);
}
@Override
public String toString() {
String toStr = "";
for (int i = 0; i < size; i++) {
toStr += (arrays[i] + "\n");
}
return toStr;
}
}