-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectArrayList.java
More file actions
142 lines (129 loc) · 4.34 KB
/
ObjectArrayList.java
File metadata and controls
142 lines (129 loc) · 4.34 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
package beanbags;
import java.io.Serializable;
/**
* Stores {@link Object} items in an array list which is more efficent in
* adding at the cost of more storage. Note that the internal representation
* does not decrease the array size stored, only increase capacity. It should
* not be used to store null references.
*
* @author Jonathan Fieldsend
* @version 1.2
*/
public class ObjectArrayList implements Serializable
{
private Object[] array;
private int elementsUsed;
/**
* Creates initial instance of an ObjectArrayList with no contents
*/
public ObjectArrayList() {
this.array = new Object[10];
this.elementsUsed = 0;
}
/**
* Method adds the argument to the end of the list. <code>null</code> reference
* elements are not supported, so ensure that you do not add them.
*
* @param o Object instance to be added
*/
public void add(Object o) {
if (this.elementsUsed == this.array.length)
this.resizeArray();
this.array[elementsUsed] = o;
this.elementsUsed++;
}
/*
* Method doubles the capacity of the array
*/
private void resizeArray(){
Object[] tempArray = new Object[this.array.length*2];
// Efficiently copy all the elements of array into tempArray
System.arraycopy(this.array,0,tempArray,0,this.array.length);
this.array = tempArray;
}
/**
* Method returns the element of the list at the index provided, will
* return <code>null</code> if the index is invalid
*
* @param index index of element in list to be returned
* @return Object at corresponding index
*/
public Object get(int index) {
if (this.isInvalid(index))
return null;
return this.array[index];
}
/*
* Checks validity of index given current range, returns true if not valid
*/
private boolean isInvalid(int index) {
return ((index < 0) || (index >= this.elementsUsed));
}
/**
* Method removes the argument if it is contained in the list and returns <code>true</true>
* if it is successful. It will return <code>false</code> if <code>o</code> is not contained
* in the list and therefore not removed
*
* @param index index of element in list to be returned
* @return Object at corresponding index
*
*/
public boolean remove(Object o) {
for (int i = 0; i < this.elementsUsed; i++) {
if (this.array[i].equals(o)) {
this.contract(i);
return true;
}
}
return false;
}
/*
* Method contracts effectively removing the index item. If item beyond range of
* array, returns false, otherwise returns true on successful removal
*/
private void contract(int index) {
// Efficiently copy all the elements of array beyond index down one
// space, effectively removing the index element
System.arraycopy(this.array,index+1,this.array,index,this.elementsUsed-(index+1));
this.elementsUsed--;
}
/**
* Method removes the element of the list at the index provided, will
* return <code>null</code> if the index is invalid. Otherwise will
* return the instance removed
*
* @param index index of element in list to be returned
* @return Object at corresponding index
*
*/
public Object remove(int index) {
if (this.isInvalid(index))
return null;
Object value = this.get(index);
this.contract(index);
return value;
}
/**
* Method replaces the element of the list at the index provided, will
* return <code>false</code> if the index is invalid, otherwise will return true.
*
* @param o object to be placed in the list
* @param index index of element in list to be replaced
* @return true if sucessfully replaced, otherwise false if index is
* out of the range of stored data
*/
public boolean replace(Object o, int index) {
if (this.isInvalid(index))
return false;
this.array[index] = o;
return true;
}
/**
* Method returns the total number of elements in the list
*
* @return number of elements in the list
*/
public int size(){
return this.elementsUsed;
}
}