forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from acd28705/master
完成热身作业,并完成了ArrayList的单元测试
- Loading branch information
Showing
9 changed files
with
543 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
group20/286166752/src/tests/wiki/liven/code/dataStructures/ArrayListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package wiki.liven.code.dataStructures; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
/** | ||
* Created by leven on 2017/2/26. | ||
*/ | ||
public class ArrayListTest { | ||
|
||
private ArrayList arrayList; | ||
|
||
|
||
@Before | ||
public void init(){ | ||
arrayList = new ArrayList(); | ||
} | ||
|
||
|
||
@Test | ||
public void add() throws Exception { | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
System.out.println(arrayList.toString()); | ||
} | ||
|
||
@Test | ||
public void add1() throws Exception { | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
System.out.println(arrayList.size()); | ||
arrayList.add(1,"星际游侠"); | ||
System.out.println(arrayList.toString()); | ||
} | ||
|
||
@Test | ||
public void get() throws Exception { | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
System.out.println(arrayList.get(1)); | ||
} | ||
|
||
@Test | ||
public void remove() throws Exception { | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
System.out.println(arrayList.remove(1)); | ||
System.out.println(arrayList.toString()); | ||
} | ||
|
||
@Test | ||
public void size() throws Exception { | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
arrayList.add("梅子黄时雨"); | ||
arrayList.add("行下一首歌"); | ||
arrayList.add("AmberTseng"); | ||
System.out.println(arrayList.size()); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
group20/286166752/src/tests/wiki/liven/code/dataStructures/LinkedListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package wiki.liven.code.dataStructures; | ||
|
||
import org.junit.Test; | ||
|
||
|
||
/** | ||
* Created by leven on 2017/2/26. | ||
*/ | ||
public class LinkedListTest { | ||
@Test | ||
public void add() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void add1() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void get() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void remove() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void size() throws Exception { | ||
|
||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
group20/286166752/src/tests/wiki/liven/code/dataStructures/QueueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package wiki.liven.code.dataStructures; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Created by leven on 2017/2/26. | ||
*/ | ||
public class QueueTest { | ||
@Test | ||
public void queueEmpty() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void enQueue() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void deQueue() throws Exception { | ||
|
||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
group20/286166752/src/tests/wiki/liven/code/dataStructures/StackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package wiki.liven.code.dataStructures; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Created by leven on 2017/2/26. | ||
*/ | ||
public class StackTest { | ||
@Test | ||
public void push() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void pop() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void getTop() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void stackEmpty() throws Exception { | ||
|
||
} | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
group20/286166752/src/wiki/liven/code/dataStructures/ArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package wiki.liven.code.dataStructures; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by leven on 2017/2/21. | ||
*/ | ||
public class ArrayList implements List{ | ||
|
||
/** | ||
* 列表中元素的个数 | ||
*/ | ||
private int size = 0; | ||
private int maxSize = 10; | ||
/** | ||
* 初始数组 | ||
*/ | ||
private Object[] elementData = new Object[maxSize]; | ||
|
||
/** | ||
* 在指定的位置i插入元素O | ||
* 插入元素,判断当前列表中元素的个数, | ||
* 当size==100,则需要扩张数组 | ||
* 当size<100,则使用初始数组完成插入操作 | ||
* 插入操作: | ||
* 从最后一个元素开始,往后移动一位,直到到index为止. | ||
* @param index | ||
* @param o | ||
*/ | ||
@Override | ||
public void add(int index, Object o) { | ||
if(index<0||index>size-1) | ||
throw new IndexOutOfBoundsException("数组下标越界异常。"); | ||
if (size==maxSize){ | ||
Object[] targt = new Object[++maxSize]; | ||
System.arraycopy(elementData,0,targt,0,elementData.length); | ||
for (int j = targt.length-2;j>=index;j--){ | ||
targt[j+1] = targt[j]; | ||
} | ||
targt[index] = o; | ||
size++; | ||
elementData = targt; | ||
}else if(size<maxSize){ | ||
for (int j = size-1;j>=index;j--){ | ||
elementData[j+1] = elementData[j]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
} | ||
|
||
/** | ||
* 追加元素 | ||
* @param o | ||
*/ | ||
@Override | ||
public void add(Object o) { | ||
if (size==maxSize){ | ||
Object[] targt = new Object[++maxSize]; | ||
System.arraycopy(elementData,0,targt,0,elementData.length); | ||
targt[maxSize-1] = o; | ||
size++; | ||
elementData = targt; | ||
}else if(size<maxSize){ | ||
elementData[size] = o; | ||
size++; | ||
} | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
if(index<0||index>size-1) | ||
throw new IndexOutOfBoundsException("数组下标越界异常"); | ||
Object o= elementData[index]; | ||
return o; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index<0||index>size-1) | ||
throw new IndexOutOfBoundsException("数组下表越界异常"); | ||
Object temp = elementData[index]; | ||
for (int i = index;i<=size-1;i++){ | ||
elementData[i] = elementData[i+1]; | ||
} | ||
size--; | ||
return temp; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "ArrayList{" + | ||
"elementData=" + Arrays.toString(elementData) + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.