Skip to content

Commit 794fea5

Browse files
committed
ArrayList(The ikook implementation)
1 parent 96e69df commit 794fea5

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.ikook.basic_data_structure;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.Date;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
/**
11+
* 此单元测试只测试了正常情况,一些异常情况没有测试。
12+
* @author ikook
13+
*/
14+
public class MyArrayListTest {
15+
16+
private MyArrayList list;
17+
18+
@Before
19+
public void setUp() {
20+
list = new MyArrayList();
21+
list.add("111");
22+
list.add("222");
23+
list.add(33);
24+
list.add("444");
25+
list.add(new Date());
26+
list.add("666");
27+
list.add("777");
28+
list.add("888");
29+
list.add("999");
30+
}
31+
32+
@Test
33+
public void testAdd() {
34+
//测试add(Object obj)方法
35+
list.add(100);
36+
assertEquals(10, list.size());
37+
38+
//测试add(int index, Object obj)方法
39+
list.add(3, 444);
40+
assertEquals(444, list.get(3));
41+
42+
assertEquals("444", list.get(4));
43+
assertEquals(11, list.size());
44+
}
45+
46+
@Test
47+
public void testIsEmpty() {
48+
49+
assertEquals(false, list.isEmpty());
50+
}
51+
52+
@Test
53+
public void testGet() {
54+
assertEquals(new Date(), list.get(4));
55+
}
56+
57+
@Test
58+
public void testRemove() {
59+
60+
// 测试remove(int index)方法
61+
assertEquals(33, list.remove(2));
62+
assertEquals("444", list.get(2));
63+
64+
// 测试remove(Object obj)方法
65+
assertEquals(true, list.remove("222"));
66+
assertEquals("444", list.get(1));
67+
}
68+
69+
@Test
70+
public void testSet() {
71+
assertEquals(33, list.set(2, "333"));
72+
assertEquals("333", list.get(2));
73+
}
74+
75+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.ikook.basic_data_structure;
2+
3+
/**
4+
* @author ikook; QQ号码: 935542673
5+
*/
6+
public class MyArrayList implements MyList{
7+
8+
private Object[] elementData;
9+
10+
private int size;
11+
12+
/**
13+
* 使Object[]的长度默认为10;
14+
*/
15+
public MyArrayList() {
16+
this(10);
17+
}
18+
19+
/**
20+
* 在构造函数中初始化集合的长度
21+
* @param initialCapacity
22+
*/
23+
public MyArrayList(int initialCapacity) {
24+
if(initialCapacity < 0) {
25+
try {
26+
throw new Exception();
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
this.elementData = new Object[initialCapacity];
32+
}
33+
34+
/**
35+
* 在集合中添加元素
36+
* @param obj
37+
*/
38+
public void add(Object obj) {
39+
40+
ensureCapacity();
41+
elementData[size++] = obj;
42+
43+
}
44+
45+
/**
46+
* 添加元素到集合的指定位置
47+
* @param index
48+
* @param obj
49+
*/
50+
public void add(int index, Object obj) {
51+
rangeCheck(index);
52+
ensureCapacity();
53+
54+
System.arraycopy(elementData, index, elementData, index + 1, size-index);
55+
elementData[index] = obj;
56+
size++;
57+
}
58+
59+
/**
60+
* 返回集合的长度
61+
* @return
62+
*/
63+
public int size() {
64+
return size;
65+
}
66+
67+
/**
68+
* 判断集合是非为空
69+
* @return
70+
*/
71+
public boolean isEmpty() {
72+
return size == 0;
73+
}
74+
75+
/**
76+
* 获取集合指定位置的元素
77+
* @param index
78+
* @return
79+
*/
80+
public Object get(int index) {
81+
rangeCheck(index);
82+
return elementData[index];
83+
}
84+
85+
/**
86+
* 删除指定位置的对象
87+
* @param index
88+
*/
89+
public Object remove(int index) {
90+
91+
rangeCheck(index);
92+
93+
Object oldValue = elementData[index];
94+
95+
int numMoved = size - index - 1;
96+
if (numMoved > 0){
97+
System.arraycopy(elementData, index+1, elementData, index,
98+
numMoved);
99+
}
100+
elementData[--size] = null;
101+
102+
return oldValue;
103+
}
104+
105+
/**
106+
* 删除指定的对象(Object 对象)
107+
* @param obj
108+
*/
109+
public boolean remove(Object obj){
110+
for(int i = 0; i < size; i++) {
111+
if(get(i).equals(obj)) {
112+
remove(i);
113+
return true;
114+
}
115+
}
116+
return false;
117+
}
118+
119+
/**
120+
* 更改集合中指定位置的元素,并返回原来的对象
121+
* @param index
122+
* @param obj
123+
* @return
124+
*/
125+
public Object set(int index, Object obj) {
126+
rangeCheck(index);
127+
128+
Object oldValue = elementData[index];
129+
elementData[index] = obj;
130+
131+
return oldValue;
132+
}
133+
134+
/**
135+
* 集合扩容封装类
136+
*/
137+
private void ensureCapacity() {
138+
if(size == elementData.length) {
139+
Object[] newArray = new Object[size * 2 + 1];
140+
System.arraycopy(elementData, 0, newArray, 0, elementData.length);
141+
elementData = newArray;
142+
}
143+
}
144+
145+
/**
146+
* 判断集合范围是否越界的封装类
147+
* @param index
148+
*/
149+
private void rangeCheck(int index) {
150+
if(index < 0 || index >= size) {
151+
try {
152+
throw new Exception("索引异常");
153+
} catch (Exception e) {
154+
e.printStackTrace();
155+
}
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)