-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayBasedList.java
More file actions
111 lines (82 loc) · 2.45 KB
/
ArrayBasedList.java
File metadata and controls
111 lines (82 loc) · 2.45 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
// 4
public class ArrayBasedList {
//fields
Object []arr; //object general datatype, can store anything
int n;
//constructor for initial value
public ArrayBasedList(){
arr = new Object[10]; //initial number, better start with small num
n = 0; //count of elements because didnt add anything yet
}
public void set(int i, Object x){
arr[i] = x;
}
public Object get(int i){
return arr[i];
}
public int size(){
return n;
}
public void add(int i, Object x){
//check is array is full
if(n == arr.length){
resize();
}
//shifting
for(int j = n; j > i ;j--) {
arr[j] = arr[j-1];
}
arr[i] = x;
n++;
}
public Object remove(int i){
//store in a variable
Object x = arr[i];
for(int j = i; j < n -1;j++) {
arr[j] = arr[j+1];
}
n--;
if(n < arr.length){
resize();
}
return x;
}
public void resize(){
Object arr2[] = new Object[n*2];
//copy elements to expanded array
for(int i = 0; i < n;i++){
arr2[i] = arr[i];
}
//define that this array is the new one
arr = arr2;
}
public void push (Object x)
{
add (n,x);
}
public Object pop()
{
return remove (n - 1);
}
public static void main(String[] args) {
ArrayBasedList list = new ArrayBasedList();
list.add(0, "Mohammad");
list.add(0, "Layal");
list.add(1, "Celine");
list.add(1, "Dana");
list.add(0, "Jouri");
for (int i = 0 ; i < list.size() ; i ++){
System.out.println(list.get(i));
}
list.remove(4);
System.out.println("After removing: ");
for (int i = 0 ; i < list.size() ; i ++){
System.out.println(list.get(i));
}
list.push("A");
list.push("B");
list.push("C");
System.out.println("After popping: ");
System.out.println(list.pop());
}
}