-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArrrayList
More file actions
123 lines (100 loc) · 2.62 KB
/
MyArrrayList
File metadata and controls
123 lines (100 loc) · 2.62 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
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.*;
public class MyArrayList <T>{
private Object Arr[];
private int capacity;
int size = 0;
public MyArrayList(){
capacity = 10;
Arr = new Object[capacity];
}
int getSize(){
return size;
}
public void Add(T value){
if(size == capacity) Resize();
Arr[size] = value;
size++;
}
public void Add(int index,T value){
Add(value);
for (int i = this.size-1; i > index; i--) {
Object temp = Arr[i];
Arr[i] = Arr[i-1];
Arr[i-1] = temp;
}
}
void AddAll(MyArrayList<T> arr){
for (int i = 0; i < arr.size; i++)
Add(arr.get(i));
}
public void Resize(){
capacity*=2;
Arr = Arrays.copyOf(Arr,size*2);
}
public void clear(){
this.size = 0;
}
public boolean contains(T value){
return indexOf(value) != -1;
}
public int indexOf(T value){
for (int i = 0; i < size; i++)
if(Arr[i] == value)
return i;
return -1;
}
void set(int index, T value){
if(index > size || index < 0)
throw new ArrayIndexOutOfBoundsException("Index : " + index);
Arr[index] = value;
}
public T get(int index){
if(index < 0 || index > size)
throw new ArrayIndexOutOfBoundsException("Index : " + index);
return (T) Arr[index];
}
public T Remove(){
T temp = (T)Arr[size-1];
Arr[size-1] = null;
size--;
return temp;
}
public T Remove(int index){
if(index < 0 || index > size)
throw new ArrayIndexOutOfBoundsException("Index : " + index);
T temp = (T) Arr[index];
for(int i = index; i < size; i++)
Arr[i] = Arr[i + 1];
size--;
return temp;
}
void revers(){
for (int i = 0; i < size/2; i++) {
T temp = (T) Arr[i];
Arr[i] = Arr[size-1-i];
Arr[size-1-i] = temp;
}
}
public boolean isEmpty(){
return this.size == 0;
}
public boolean equals(Object o){
MyArrayList arr = (MyArrayList)o;
if(arr.size == this.size){
for(int i=0;i<this.size;i++)
if(arr.get(i) != Arr[i])
return false;
return true;
}
else return false;
}
@Override
public String toString() {
String s = "[";
for(int i=0;i<size;i++){
s+=Arr[i];
if(i<size-1) s+=" , ";
}
return s+"]";
}
}