Skip to content

Commit d07bbe3

Browse files
committed
添加ArrayList的Demo以及自己写的简单的ArrayList
1 parent f6acce4 commit d07bbe3

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.byhieg.collectiontutorial.listtutorial;
2+
3+
/**
4+
* Created by byhieg on 17/2/7.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class MyArrayList<E> {
8+
9+
10+
private final static int DEFAULT_CAPACITY = 10;
11+
private int size;
12+
13+
private Object [] elementData;
14+
15+
//默认的构造方法
16+
public MyArrayList(){
17+
this(DEFAULT_CAPACITY);
18+
}
19+
20+
public MyArrayList(int size){
21+
if (size < 0){
22+
throw new IllegalArgumentException("默认的大小" + size);
23+
}else{
24+
elementData = new Object[size];
25+
}
26+
}
27+
28+
29+
}

src/test/java/cn/byhieg/collectiontutorialtest/ArrayListDemoTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import cn.byhieg.collectiontutorial.listtutorial.ArrayListDemo;
44
import junit.framework.TestCase;
5-
65
import java.util.Iterator;
76

87
/**
@@ -17,10 +16,17 @@ public void testList() throws Exception{
1716
for (int i = 0 ;i < 10 ; i++){
1817
demo.getListA().add(i);
1918
}
20-
Iterator iterator = demo.getListA().iterator();
21-
while (iterator.hasNext()) {
22-
System.out.println(iterator.next());
19+
Iterator iteratorA = demo.getListA().iterator();
20+
while (iteratorA.hasNext()) {
21+
System.out.println(iteratorA.next());
22+
}
23+
24+
demo.setListC(demo.getListA());
25+
Iterator iteratorC = demo.getListC().iterator();
26+
while (iteratorC.hasNext()) {
27+
System.out.println(iteratorC.next());
2328
}
29+
2430
}
2531

2632
}

0 commit comments

Comments
 (0)