-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectList.java
More file actions
84 lines (77 loc) · 2.26 KB
/
ObjectList.java
File metadata and controls
84 lines (77 loc) · 2.26 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
import java.io.*;
/**
*
*/
public class ObjectList implements Serializable
{
private Object[] objectlist;
private int total;// counter
public ObjectList(int size)
{
this.objectlist = new Object[size]; // set size
this.total = 0;
}
public boolean add(Object p){
// to add an object
//ClubFullException would be inmplemented here if full
if (total < objectlist.length) {
objectlist[total] = p;
total++;
return true;
} else {
int newLength = objectlist.length + 1;
Object[] newArray = new Object[newLength];
// Copy elements from the old array to new
System.arraycopy(objectlist, 0, newArray, 0, objectlist.length);
// Update the reference to the new array
objectlist = newArray;
// Add the new object to the resized array
objectlist[total] = p;
total++; // incement the total
return true;
}
}
public boolean isEmpty() {
return total == 0;
}
public boolean isFull() {
if(total == objectlist.length)
{
// if full return true
//ClubFullException would be inmplemented here
return true;
}
else {
// else false
return false;
}
}
public Object getObject(int k) {
// get object
if (k >= 0 && k < total) {
return objectlist[k]; // from object list get k
} else {
return null;
}
}
public int getTotal() {
return total;
}
public boolean remove(int k)
{
// remove method
if (!isEmpty() && k >= 0 && k < total) {
// Start a loop from index 'k' to the second last element in list
for (int i = k; i < total - 1; i++) {
// Shift each element to the left
objectlist[i] = objectlist[i + 1];
}
// Set last in list to null to remove copy element
objectlist[total - 1] = null;
total--; // decrement the total
return true; // return true as removed worked
} else {
return false; // return false if not worked
}
}
}