Repository navigation
Expand file tree
/
Copy pathArrayList.java
More file actions
163 lines (132 loc) · 3.01 KB
/
Copy pathArrayList.java
File metadata and controls
163 lines (132 loc) · 3.01 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
public class ArrayList implements List {
protected Object[] listArray;
protected int size;
protected static int INITIAL_SIZE = 5;
private ReturnObject obj;
public ArrayList() {
listArray = new Object[INITIAL_SIZE];
size = 0;
}
public boolean isEmpty() {
if(size == 0) {
return true;
} else {
return false;
}
}
public int size() {
return size;
}
/**
* returns the object at the specified position
*/
public ReturnObject get(int index) {
if (isEmpty()) {
obj = new ReturnObjectImpl(ErrorMessage.EMPTY_STRUCTURE);
obj.getError();
return obj;
} else if (index < 0 || index >= size) {
obj = new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
obj.getError();
return obj;
} else {
obj = new ReturnObjectImpl(listArray[index]);
System.out.println("" + obj.getReturnValue());
return obj;
}
/**
* adds an item to the end of the list
*/
}
public ReturnObject add(Object item) {
if (item == null) {
obj = new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT);
obj.getError();
return obj;
}
if (isAlmostFull()) {
reserveMoreMemory();
}
listArray[size] = item;
size++;
obj = new ReturnObjectImpl(item);
obj.getReturnValue();
return obj;
}
/**
* adds an object to a specified position in the list
*
*/
public ReturnObject add(int index, Object item) {
if (index < 0 || index > size) {
obj = new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
obj.getError();
return obj;
}
if (item == null) {
obj = new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT);
obj.getError();
return obj;
}
if (isAlmostFull()) {
reserveMoreMemory();
}
if (index == size) {
add(item);
return null;
}
for (int i = 0; i < size - index; i++) {
listArray[size - i] = listArray[size - 1 - i];
}
listArray[index] = item;
size++;
obj = new ReturnObjectImpl(item);
obj.getReturnValue();
return obj;
}
/**
* removes an item; if the item to be removed is the last item in the list then the correspinding list reference becomes null.
*/
public ReturnObject remove(int index) {
if (isEmpty()) {
obj = new ReturnObjectImpl(ErrorMessage.EMPTY_STRUCTURE);
obj.getError();
return obj;
} else if (index < 0 || index >= size) {
obj = new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
obj.getError();
return obj;
} else {
size--;
if (index == size) {
listArray[index] = null;
} else {
for (int i = 0; i < size - index; i++) {
listArray[index + i] = listArray[index + i + 1];
}
}
obj = new ReturnObjectImpl(listArray[index]);
return obj;
}
}
/**
* checks if there is a space in the array
*/
private boolean isAlmostFull() {
if(listArray.length - size < 1) {
return true;
} else {
return false;
}
}
/**
* makes a new array twice the size of the current one
*/
private void reserveMoreMemory() {
Object[] biggerArray = new Object[size * 2];
for (int i = 0; i < listArray.length; i++) {
biggerArray[i] = listArray[i];
}
listArray = biggerArray;
}
}