Skip to content

Commit 44c0f95

Browse files
committed
完成作业,完成ArrayList的单元测试
1 parent d9e9ba4 commit 44c0f95

File tree

8 files changed

+307
-25
lines changed

8 files changed

+307
-25
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package wiki.liven.code.dataStructures;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
7+
/**
8+
* Created by leven on 2017/2/26.
9+
*/
10+
public class ArrayListTest {
11+
12+
private ArrayList arrayList;
13+
14+
15+
@Before
16+
public void init(){
17+
arrayList = new ArrayList();
18+
}
19+
20+
21+
@Test
22+
public void add() throws Exception {
23+
arrayList.add("梅子黄时雨");
24+
arrayList.add("行下一首歌");
25+
arrayList.add("AmberTseng");
26+
arrayList.add("梅子黄时雨");
27+
arrayList.add("行下一首歌");
28+
arrayList.add("AmberTseng");
29+
arrayList.add("梅子黄时雨");
30+
arrayList.add("行下一首歌");
31+
arrayList.add("AmberTseng");
32+
arrayList.add("梅子黄时雨");
33+
arrayList.add("行下一首歌");
34+
arrayList.add("AmberTseng");
35+
System.out.println(arrayList.toString());
36+
}
37+
38+
@Test
39+
public void add1() throws Exception {
40+
arrayList.add("梅子黄时雨");
41+
arrayList.add("行下一首歌");
42+
System.out.println(arrayList.size());
43+
arrayList.add(1,"星际游侠");
44+
System.out.println(arrayList.toString());
45+
}
46+
47+
@Test
48+
public void get() throws Exception {
49+
arrayList.add("梅子黄时雨");
50+
arrayList.add("行下一首歌");
51+
arrayList.add("AmberTseng");
52+
arrayList.add("梅子黄时雨");
53+
arrayList.add("行下一首歌");
54+
arrayList.add("AmberTseng");
55+
arrayList.add("梅子黄时雨");
56+
arrayList.add("行下一首歌");
57+
arrayList.add("AmberTseng");
58+
arrayList.add("梅子黄时雨");
59+
arrayList.add("行下一首歌");
60+
arrayList.add("AmberTseng");
61+
System.out.println(arrayList.get(1));
62+
}
63+
64+
@Test
65+
public void remove() throws Exception {
66+
arrayList.add("梅子黄时雨");
67+
arrayList.add("行下一首歌");
68+
System.out.println(arrayList.remove(1));
69+
System.out.println(arrayList.toString());
70+
}
71+
72+
@Test
73+
public void size() throws Exception {
74+
arrayList.add("梅子黄时雨");
75+
arrayList.add("行下一首歌");
76+
arrayList.add("AmberTseng");
77+
arrayList.add("梅子黄时雨");
78+
arrayList.add("行下一首歌");
79+
arrayList.add("AmberTseng");
80+
arrayList.add("梅子黄时雨");
81+
arrayList.add("行下一首歌");
82+
arrayList.add("AmberTseng");
83+
arrayList.add("梅子黄时雨");
84+
arrayList.add("行下一首歌");
85+
arrayList.add("AmberTseng");
86+
System.out.println(arrayList.size());
87+
}
88+
89+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package wiki.liven.code.dataStructures;
2+
3+
import org.junit.Test;
4+
5+
6+
/**
7+
* Created by leven on 2017/2/26.
8+
*/
9+
public class LinkedListTest {
10+
@Test
11+
public void add() throws Exception {
12+
13+
}
14+
15+
@Test
16+
public void add1() throws Exception {
17+
18+
}
19+
20+
@Test
21+
public void get() throws Exception {
22+
23+
}
24+
25+
@Test
26+
public void remove() throws Exception {
27+
28+
}
29+
30+
@Test
31+
public void size() throws Exception {
32+
33+
}
34+
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package wiki.liven.code.dataStructures;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* Created by leven on 2017/2/26.
7+
*/
8+
public class QueueTest {
9+
@Test
10+
public void queueEmpty() throws Exception {
11+
12+
}
13+
14+
@Test
15+
public void enQueue() throws Exception {
16+
17+
}
18+
19+
@Test
20+
public void deQueue() throws Exception {
21+
22+
}
23+
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package wiki.liven.code.dataStructures;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* Created by leven on 2017/2/26.
7+
*/
8+
public class StackTest {
9+
@Test
10+
public void push() throws Exception {
11+
12+
}
13+
14+
@Test
15+
public void pop() throws Exception {
16+
17+
}
18+
19+
@Test
20+
public void getTop() throws Exception {
21+
22+
}
23+
24+
@Test
25+
public void stackEmpty() throws Exception {
26+
27+
}
28+
29+
}

group20/286166752/src/wiki/liven/code/dataStructures/ArrayList.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package wiki.liven.code.dataStructures;
22

3+
import java.util.Arrays;
4+
35
/**
46
* Created by leven on 2017/2/21.
57
*/
@@ -31,15 +33,16 @@ public void add(int index, Object o) {
3133
throw new IndexOutOfBoundsException("数组下标越界异常。");
3234
if (size==maxSize){
3335
Object[] targt = new Object[++maxSize];
34-
System.arraycopy(elementData,0,targt,0,maxSize);
35-
for (int j = targt.length;j>=index;j--){
36-
targt[j-1] = targt[j-2];
36+
System.arraycopy(elementData,0,targt,0,elementData.length);
37+
for (int j = targt.length-2;j>=index;j--){
38+
targt[j+1] = targt[j];
3739
}
3840
targt[index] = o;
3941
size++;
42+
elementData = targt;
4043
}else if(size<maxSize){
41-
for (int j = elementData.length;j>=index;j--){
42-
elementData[j-1] = elementData[j-2];
44+
for (int j = size-1;j>=index;j--){
45+
elementData[j+1] = elementData[j];
4346
}
4447
elementData[index] = o;
4548
size++;
@@ -54,11 +57,12 @@ public void add(int index, Object o) {
5457
public void add(Object o) {
5558
if (size==maxSize){
5659
Object[] targt = new Object[++maxSize];
57-
System.arraycopy(elementData,0,targt,0,maxSize);
60+
System.arraycopy(elementData,0,targt,0,elementData.length);
5861
targt[maxSize-1] = o;
5962
size++;
63+
elementData = targt;
6064
}else if(size<maxSize){
61-
elementData[size-1] = o;
65+
elementData[size] = o;
6266
size++;
6367
}
6468
}
@@ -76,7 +80,7 @@ public Object remove(int index) {
7680
if (index<0||index>size-1)
7781
throw new IndexOutOfBoundsException("数组下表越界异常");
7882
Object temp = elementData[index];
79-
for (int i = index;i>size-1;i++){
83+
for (int i = index;i<=size-1;i++){
8084
elementData[i] = elementData[i+1];
8185
}
8286
size--;
@@ -89,6 +93,10 @@ public int size() {
8993
}
9094

9195

92-
93-
96+
@Override
97+
public String toString() {
98+
return "ArrayList{" +
99+
"elementData=" + Arrays.toString(elementData) +
100+
'}';
101+
}
94102
}

group20/286166752/src/wiki/liven/code/dataStructures/Queue.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,63 @@
22

33
/**
44
* Created by leven on 2017/2/21.
5+
* 队列
6+
* 只允许一端进行出队操作,另一端进行入队操作。
7+
* 特性是:先进先出
8+
* 本质上是一种操作受限制的线性表
9+
* 本实现线性表采用自己实现的ArrayList,
10+
* ArrayList理论上会受到JVM分配的内存大小,从而其空间会有上限。
11+
* 但是,该错误,将有JVM抛出。
12+
* 所以,本实现,队列忽略执行判断满队操作。
513
*/
614
public class Queue {
15+
16+
private ArrayList list;
17+
private int head;//队头指针
18+
private int foot;//队尾指针
19+
20+
21+
public Queue(){
22+
head = foot = 0;
23+
}
24+
25+
/**
26+
* 判空
27+
* @return
28+
*/
29+
public boolean queueEmpty(){
30+
if (head==0&&foot==0){
31+
return true;
32+
}else {
33+
return false;
34+
}
35+
}
36+
37+
/**
38+
* 入队列
39+
* 1.先把元素放置到队列中
40+
* 2.将队尾指针加1
41+
* @param o
42+
*/
43+
public void enQueue(Object o){
44+
list.add(foot,o);
45+
foot++;
46+
}
47+
48+
/**
49+
* 删除队头元素
50+
* 0.先判断队列是否为空
51+
* 1.先取出队头的元素
52+
* 2.然后将队头的指针加1
53+
* @return
54+
*/
55+
public int deQueue(){
56+
if (queueEmpty()==true)
57+
throw new IndexOutOfBoundsException("队列为空,无法执行该操作。");
58+
list.remove(head);
59+
return head++;
60+
}
61+
62+
63+
764
}

group20/286166752/src/wiki/liven/code/dataStructures/Stack.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,61 @@
22

33
/**
44
* Created by leven on 2017/2/21.
5+
* 栈:只允许在一端进行删除或者增加操作的线性表。
6+
* 本实现,采用ArrayList。
57
*/
68
public class Stack {
9+
10+
11+
private ArrayList list;//线性表
12+
private int top = -1;//栈顶指针,默认指向栈低
13+
14+
15+
/**
16+
* 元素进栈
17+
* @param o
18+
* 1.指针先加1
19+
* 2.将元素放入栈中
20+
*
21+
*/
22+
public void push(Object o){
23+
top++;
24+
list.add(o);
25+
}
26+
27+
/**
28+
* 栈顶元素出栈,返回栈顶指针的值
29+
* @return
30+
*/
31+
public int pop(){
32+
if (top==-1)
33+
throw new IndexOutOfBoundsException("栈为空,无法执行出栈操作。");
34+
list.remove(top);
35+
top--;
36+
return top;
37+
}
38+
39+
/**
40+
* 获取栈顶元素的值
41+
* @return
42+
*/
43+
public Object getTop() {
44+
if (top==-1)
45+
throw new IndexOutOfBoundsException("栈为空,无法执行出栈操作。");
46+
Object o = list.get(top);
47+
return o;
48+
}
49+
50+
/**
51+
* 判空
52+
* @return
53+
*/
54+
public boolean stackEmpty(){
55+
if (top==-1){
56+
return true;
57+
}else {
58+
return false;
59+
}
60+
}
61+
762
}

group20/286166752/src/wiki/liven/code/test/SomeDemos.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)