Skip to content

Commit 317f47a

Browse files
ThinkPadThinkPad
authored andcommitted
ArrayList提交
1 parent 2ca2147 commit 317f47a

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,131 @@
11
package com.my.list;
22

3+
/**
4+
* 实现ArrayList
5+
*
6+
*/
37
public class ArrayList {
8+
//初始化容量
9+
private static final int INIT = 5;
10+
//增长因子
11+
private static final double GROWTH = 0.5;
12+
13+
//初始化数组
14+
Object[] baseArr = new Object[INIT];
15+
//当前下标
16+
int currentIndex = 0;
17+
//最大下标
18+
int maxIndex = INIT-1;
19+
20+
/**
21+
* 添加元素
22+
* @param obj
23+
* @return
24+
*/
25+
public Object add(Object obj){
26+
if(judgeCapacity()){
27+
baseArr = arrayDilatation(baseArr, GROWTH);
28+
maxIndex = baseArr.length-1;
29+
}
30+
baseArr[currentIndex] = obj;
31+
++currentIndex;
32+
return obj;
33+
}
34+
35+
/**
36+
* 指定下标添加元素
37+
* @param index
38+
* @param obj
39+
* @return
40+
*/
41+
public Object add(int index , Object obj){
42+
if (index < 0 || index > currentIndex) {
43+
throw new IndexOutOfBoundsException();
44+
}
45+
Object[] dest = new Object[currentIndex+1];
46+
System.arraycopy(baseArr, 0, dest, 0, index);
47+
dest[index] = obj;
48+
System.arraycopy(baseArr, index, dest, index+1, currentIndex-index);
49+
++currentIndex;
50+
baseArr = dest;
51+
return obj;
52+
}
53+
54+
/**
55+
* 指定下标删除元素
56+
* @param index
57+
* @return
58+
*/
59+
public Object remove(int index){
60+
if (index < 0 || index >= currentIndex) {
61+
throw new IndexOutOfBoundsException();
62+
}
63+
Object object = baseArr[index];
64+
Object[] dest = new Object[currentIndex];
65+
System.arraycopy(baseArr, 0, dest, 0, index);
66+
System.arraycopy(baseArr, index+1, dest, index, currentIndex-index-1);
67+
--currentIndex;
68+
baseArr = dest;
69+
return object;
70+
}
71+
72+
/**
73+
* 根据下标获取元素
74+
* @param index
75+
* @return
76+
*/
77+
public Object get(int index){
78+
79+
return baseArr[index];
80+
}
81+
82+
/**
83+
* 获取集合大小
84+
* @return
85+
*/
86+
public int size(){
87+
return currentIndex;
88+
}
89+
90+
/**
91+
* 判断容量是否需要增加
92+
* @return
93+
*/
94+
public boolean judgeCapacity(){
95+
if(currentIndex > maxIndex){
96+
return true;
97+
}
98+
return false;
99+
}
100+
101+
/**
102+
* 数组扩容
103+
* @param objArr
104+
* @param groth
105+
* @return
106+
*/
107+
public static Object[] arrayDilatation(Object[] objArr , double groth){
108+
int length = objArr.length;
109+
int newLength = (int) (length * groth + length);
110+
Object[] baseArr = new Object[newLength];
111+
System.arraycopy(objArr, 0, baseArr, 0, length);
112+
return baseArr;
113+
}
114+
4115

5116
}
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.my.list;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
testArrayList();
7+
8+
9+
}
10+
11+
/**
12+
* list 测试
13+
*/
14+
public static void testArrayList(){
15+
ArrayList list = new ArrayList();
16+
list.add("123");
17+
list.add("123");
18+
list.add("123");
19+
list.add("123");
20+
list.add("123");
21+
list.add("123");
22+
23+
list.add(1, 111);
24+
25+
list.remove(1);
26+
27+
System.out.println(list.baseArr.length);
28+
System.out.println(list.size());
29+
30+
for (int i = 0; i < list.size(); i++) {
31+
Object object = list.get(i);
32+
System.out.println(object);
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)