This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList.java
149 lines (130 loc) · 2.95 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.hkt.tutorial.algorithms.ds;
import java.util.Arrays;
import java.util.Iterator;
@SuppressWarnings("unchecked")
public class ArrayList<T> implements List<T>,Iterable<T>{
private T[] arr;
private int curPos;
public ArrayList(int size) {
arr = (T[]) new Object[size];
curPos = 0;
}
@Override
public boolean add(T t) {
checkFull() ;
arr[curPos] = t;
curPos++;
return true;
}
public void addAll(T[] t) {
for(T i : t) {
add(i);
}
}
@Override
public int size() {
return curPos;
}
@Override
public boolean removeObj(T t) {
int vitri = timKiem(t);
if(vitri == -1) {
throw new RuntimeException("Not found"+ t);
} else {
for(int i = vitri; i <= curPos - 2; i++) {
arr[i] = arr[i+1];
}
curPos--;
return true;
}
}
@Override
public void clear() {
arr = (T[]) new Object[0];
curPos = 0;
}
public int timKiem(T x) {
for(int i = 0 ; i < arr.length;i++) {
if(x.equals(arr[i])) {
return i;
}
}
return -1;
}
@Override
public Iterator<T> iterator() {
return Arrays.asList(arr).subList(0, curPos).iterator();
}
@Override
public void add(int index,T element) {
if(index >= curPos) {
throw new RuntimeException("Out of array size");
}
for(int i = curPos-1; i > index; i--) {
arr[i]= arr[i-1];
}
arr[index] = element ;
curPos++;
}
@Override
public void checkFull() {
if(curPos == arr.length ) {
if(arr.length < 3) {
@SuppressWarnings("unused")
T[] newArray = (T[]) new Object [arr.length + arr.length];
}
T[] newArray = (T[]) new Object [arr.length + arr.length/3];
for(int i = 0; i < arr.length; i++) {
newArray[i] = arr[i];
}
arr = newArray;
}
}
@Override
public T get(int index) {
if(index >= arr.length) {
throw new RuntimeException("Out of array size");
}
return arr[index];
}
@Override
public int indexOf(T o) {
int index = timKiem(o);
if(index == -1) {
return -1;
}
return index;
}
@Override
public int lastIndexOf(T o) {
for(int i = arr.length - 1 ; i > 0;i++) {
if(o == arr[i]) {
return i;
}
}
return -1;
}
@Override
public T set(int index, T element) {
if(index > arr.length || index < 0) {
throw new IndexOutOfBoundsException("Out of array size");
}
arr[index] = element;
return arr[index];
}
@Override
public T removeAt(int index) {
if(index >= arr.length) {
throw new RuntimeException("Out of array size");
}
for(int i = index ;i<curPos-1;i++) {
arr[i] = arr[i+1];
}
curPos--;
return null;
}
@Override
public boolean isEmpty() {
return (size()==0);
}
}