-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
47 lines (38 loc) · 1.21 KB
/
Copy pathArrayList.java
File metadata and controls
47 lines (38 loc) · 1.21 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
/*public class ArrayList <E>{
private int size = 0;
public static int INITIAL_CAPACITY = 1;
private E[] data = (E[]) (new Object[INITIAL_CAPACITY]);
public ArrayList(){}
public int size(){ return this.size; }
private void ensureCapacity()
{
if(this.size >= this.data.length)
{
E[] newData = (E[]) (new Object[this.size * 2 + 1]);
System.arraycopy(this.data, 0, newData, 0, this.size);
this.data=newData;
}
}
public void add(E element) {
this.ensureCapacity();
this.data[this.size++] = element;
}
public E get(int index) {
return this.data[index];
}
public void clear() {
this.data = (E[]) (new Object[this.INITIAL_CAPACITY]);
this.size=0;
}
public E set(int index, E element) {
E old = this.data[index];
this.data[index] = element;
return old;
}
public boolean contains(E element) {
for (int i=0; i<size; i++)
if(element.equals(data[i]))
return true;
return false;
}
}/***End of ArrayList Definition***/