Skip to content

Commit d556b8c

Browse files
author
sheng
committed
1158154002
test01
1 parent 8cdcf8e commit d556b8c

File tree

14 files changed

+789
-0
lines changed

14 files changed

+789
-0
lines changed

group17/1158154002/.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

group17/1158154002/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

group17/1158154002/.project

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>1158154002</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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package test01.arrayList;
2+
3+
import org.junit.Test;
4+
5+
public class ArrayListTest {
6+
7+
@Test
8+
public void cetrinWArrayListTest(){
9+
CetrinwList<String> list = new CetrinwArrayList<String>();
10+
11+
list.add("a");
12+
list.add("b");
13+
list.add("c");
14+
list.add("d");
15+
16+
System.out.println("下标为3的元素为"+list.get(3));
17+
System.out.println("数组size:"+list.size());
18+
list.remove(2);
19+
System.out.print("remove后的数组size:"+list.size());
20+
21+
System.out.print("remove后的数组:");
22+
for (int i = 0; i < list.size() ; i++) {
23+
System.out.print(list.get(i)+",");
24+
}
25+
26+
list.insert(3,"gg");
27+
28+
System.out.println("");
29+
System.out.print("insert后的数组:");
30+
for (int i = 0; i < list.size() ; i++) {
31+
System.out.print(list.get(i)+",");
32+
}
33+
}
34+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package test01.arrayList;
2+
3+
public class CetrinwArrayList<E> implements CetrinwList<E> {
4+
5+
/**
6+
* 数组默认长度
7+
*/
8+
private static final int DEFAULT_SIZE=10;
9+
10+
/**
11+
* 存储队列中的元素
12+
*/
13+
private Object[] elements=null;
14+
15+
/**
16+
* 数组大小指针
17+
*/
18+
private int capacity;
19+
20+
/**
21+
* 当前游标
22+
*/
23+
private int current;
24+
25+
public CetrinwArrayList() {
26+
this(DEFAULT_SIZE);
27+
}
28+
29+
public CetrinwArrayList(int size) {
30+
if (size<0) {
31+
throw new RuntimeException("数组大小不能小于0");
32+
} else {
33+
this.elements=new Object[size];
34+
this.current=0;
35+
this.capacity=size;
36+
}
37+
}
38+
39+
@Override
40+
public E get(int index) {
41+
confirmIndex(index);
42+
return (E)elements[index];
43+
}
44+
45+
@Override
46+
public void add(E e) {
47+
confirmSize();
48+
this.elements[current]=e;
49+
this.current++;
50+
}
51+
52+
@Override
53+
public void remove(int index) {
54+
confirmIndex(index);
55+
for (int i = index; i < elements.length; i++) {
56+
if (i+1<elements.length) {
57+
elements[i]=elements[i+1];
58+
}
59+
}
60+
this.current--;
61+
}
62+
63+
@Override
64+
public void insert(int index, E e) {
65+
confirmIndex(index);
66+
for (int i = current; i >=index; i--) {
67+
elements[i+1]=elements[i];
68+
}
69+
elements[index]=e;
70+
current++;
71+
}
72+
73+
@Override
74+
public boolean contains(Object o) {
75+
for (Object object : elements) {
76+
if (object.equals(o)) {
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+
83+
@Override
84+
public int size() {
85+
return this.current;
86+
}
87+
88+
@Override
89+
public boolean isEmpty() {
90+
if (current>0) {
91+
return false;
92+
}
93+
return true;
94+
}
95+
96+
@Override
97+
public void clearList() {
98+
for (int i = 0; i < current; i++) {
99+
elements[i]=null;
100+
}
101+
}
102+
103+
/**
104+
* 确认当前数组的容量,如果满足,则不操作,如果不满足,则增加空间
105+
*/
106+
private void confirmSize(){
107+
if (this.current==this.capacity) {
108+
this.capacity=this.capacity*3/2;
109+
Object[] newElements=new Object[this.capacity];
110+
System.arraycopy(elements, 0, newElements, 0, elements.length);
111+
this.elements=newElements;
112+
}
113+
}
114+
115+
116+
/**
117+
* 判断下标是否越界
118+
*/
119+
private void confirmIndex(int index){
120+
if (index>capacity||index<0) {
121+
throw new RuntimeException("下标越界");
122+
}
123+
}
124+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package test01.arrayList;
2+
3+
public interface CetrinwList<E> {
4+
/**
5+
* 取得数据
6+
*/
7+
E get(int index);
8+
9+
/**
10+
*新增数据
11+
*/
12+
void add(E e);
13+
14+
/**
15+
* 移除数据
16+
*/
17+
void remove(int index);
18+
19+
/**
20+
* 插入数据
21+
*/
22+
void insert(int index,E e);
23+
24+
/**
25+
* 是否存在数据
26+
*/
27+
boolean contains(Object o);
28+
29+
/**
30+
* 获得List长度
31+
*/
32+
int size();
33+
34+
/**
35+
* 是否为空
36+
*/
37+
boolean isEmpty();
38+
39+
/**
40+
* 清空
41+
*/
42+
void clearList();
43+
}

0 commit comments

Comments
 (0)