-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList.java
84 lines (75 loc) · 2.18 KB
/
ArrayList.java
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
public class ArrayList {
// Implement this structure to store all Member Objects
static final int defaultCapacity = 30;
int capacity; //max number of elements this can hold
int currentSize; //number of elements this is currently holding
Member[] array;
public ArrayList() {
capacity = defaultCapacity;
array = new Member[capacity];
}
public ArrayList(int specifiedCapacity) {
capacity = specifiedCapacity;
array = new Member[capacity];
}
public int size() {
return currentSize;
}
public boolean isEmpty() {
return currentSize == 0;
}
public void set(int i, Member m) {
array[i] = m;
}
public Member get(int i) {
if (i < currentSize) {
return array[i];
}
else {
return null;
}
}
public void Add(Member m) {
if (currentSize < capacity) { //still got space
array[currentSize++] = m;
}
else {
capacity += defaultCapacity;
Member[] newArray = new Member[capacity];
for (int i = 0; i < currentSize; i++) {
newArray[i] = array[i];
}
newArray[currentSize++] = m;
array = newArray;
}
}
public void Mod(String id,boolean vipS){
//search array for existing Element ID
boolean found = false;
for(int i=0;i<currentSize;i++){
if(array[i].getID().equals(id)){
array[i].setVIP(vipS);
found = true;
}
}
//if not found print error to log
if(!found){
System.out.println("ID not found for ID: " + id);
}
else{
//print modification to log
}
}
public void Rem(int index) {//invalid name and ID
for (int i = index; i < currentSize; i++) {
array[i] = array[i+1];
}
currentSize--;
}
public void printAll() {
System.out.println("Current size = " + currentSize + "/" + capacity);
for (int i = 0; i < currentSize; i++) {
System.out.println(i + "\t" + array[i].toString());
}
}
}