Skip to content

Commit 2b8d0e2

Browse files
structure
1 parent 16dfd34 commit 2b8d0e2

File tree

16 files changed

+738
-0
lines changed

16 files changed

+738
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>datastructure</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package Collection;
2+
3+
import com.coding.basic.List;
4+
5+
import java.util.NoSuchElementException;
6+
7+
import com.coding.basic.Iterator;
8+
9+
public class ArrayList implements List {
10+
11+
private int size;
12+
private Object[] elementData;
13+
14+
public ArrayList() {
15+
size = 0;
16+
elementData = new Object[10];
17+
}
18+
19+
public ArrayList(Object o) {
20+
size = 0;
21+
elementData = new Object[10];
22+
this.add(o);
23+
}
24+
25+
public ArrayList(int initialCapacity) {
26+
size = 0;
27+
elementData = new Object[initialCapacity];
28+
}
29+
30+
@Override
31+
public void add(Object o) {
32+
if (size <= elementData.length - 1) {
33+
elementData[size] = o;
34+
size++;
35+
} else {
36+
this.extendCapacity();
37+
elementData[size] = o;
38+
size++;
39+
40+
}
41+
}
42+
43+
@Override
44+
public void add(int index, Object o) {
45+
if (index < 0) {
46+
throw new IndexOutOfBoundsException();
47+
}
48+
if (index > elementData.length - 1) {
49+
while (index > elementData.length - 1) {
50+
this.extendCapacity();
51+
}
52+
elementData[index] = o;
53+
size = index + 1;
54+
return;
55+
}
56+
57+
if (index >= size) {
58+
size = index + 1;
59+
elementData[index] = o;
60+
return;
61+
}
62+
if (index >= 0 && index < size) {
63+
this.moveRearward(index);
64+
elementData[index] = o;
65+
size++;
66+
return;
67+
}
68+
}
69+
70+
@Override
71+
public Object get(int index) {
72+
checkCapacity(index);
73+
if (index < size) {
74+
return elementData[index];
75+
}
76+
return null;
77+
}
78+
79+
@Override
80+
public Object remove(int index) {
81+
checkCapacity(index);
82+
83+
if (index == size - 1) {
84+
size--;
85+
return elementData[size - 1];
86+
}
87+
if (index < size - 1) {
88+
Object tmp = elementData[index];
89+
for (int i = index; i < size - 1; i++) {
90+
elementData[i] = elementData[i + 1];
91+
}
92+
size--;
93+
return tmp;
94+
}
95+
return null;
96+
}
97+
98+
private void checkCapacity(int index) {
99+
if (index < 0 || index >= size) {
100+
throw new IndexOutOfBoundsException();
101+
}
102+
}
103+
104+
private void extendCapacity() {
105+
Object[] elements = new Object[elementData.length + 10];
106+
for (int i = 0; i < size; i++) {
107+
elements[i] = elementData[i];
108+
}
109+
elementData = elements;
110+
111+
}
112+
113+
@Override
114+
public int size() {
115+
return size;
116+
}
117+
118+
public String toString() {
119+
StringBuilder sb = new StringBuilder();
120+
sb.append("[");
121+
for (int i = 0; i < size; i++) {
122+
sb.append(elementData[i]);
123+
sb.append(",");
124+
}
125+
sb.deleteCharAt(sb.length() - 1);
126+
sb.append("]");
127+
return sb.toString();
128+
}
129+
130+
private void moveRearward(int index) {
131+
size++;
132+
133+
if (size >= elementData.length - 1)
134+
this.extendCapacity();
135+
136+
for (int i = size - 1; i > index; i--) {
137+
elementData[i] = elementData[i - 1];
138+
}
139+
}
140+
141+
public Iterator iterator() {
142+
return new ArrayListIterator();
143+
}
144+
145+
private class ArrayListIterator implements Iterator {
146+
147+
private int pos;
148+
149+
public ArrayListIterator() {
150+
151+
pos = 0;
152+
}
153+
154+
@Override
155+
public boolean hasNext() {
156+
if (pos < size) {
157+
return true;
158+
}
159+
return false;
160+
}
161+
162+
@Override
163+
public Object next() {
164+
if (hasNext()) {
165+
return elementData[pos++];
166+
} else
167+
throw new NoSuchElementException();
168+
169+
}
170+
171+
}
172+
173+
}

0 commit comments

Comments
 (0)