-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayLists.java
More file actions
77 lines (67 loc) · 1.72 KB
/
Copy pathArrayLists.java
File metadata and controls
77 lines (67 loc) · 1.72 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
public class ArrayLists<T>{
private T[] array ;
private int index = 0;
private int size = 0;
public ArrayLists(int size){
array = (T[])new Object[size];
this.size = size;
}
public ArrayLists(){
//array = null;
// System.out.println("empty constructor is invoked");
}
public void add(T object){
if (size == 0){
array = (T[])new Object[10];
size = array.length;
}
if (index + 2 >= size){
doubleSize();
}
array[index] = object;
index++;
}
public void add(int i, T object){
if (i >= index){
System.out.println("keep index below the size : "+index);
return;
}
else {
array[i] = object;
}
}
public T get(int i){
if (i >= index){
System.out.println("index out of bound : "+index);
//exception
}
return array[i];
}
public int size(){
return index;
}
public void remove(int i){
if (i >= index){
System.out.println("index out of bound : "+index);
return;
}
if (i < index - 1){
//array[i] = array[i+1];
for (int j = i; j < index-1; j++){
array[j] = array[j+1];
}
}
index--;
}
public void doubleSize(){
T[] tempArr = (T[]) new Object[2*size];
for (int i = 0; i < size; i++){
tempArr[i] = array[i];
}
array = tempArr;
size = array.length;
}
public boolean isEmpty(){
return (index == 0);
}
}