forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #10 from CheungChanDevCoder/master
提交作业
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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,4 @@ | ||
/bin | ||
.classpath | ||
.project | ||
.settings/ |
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,54 @@ | ||
package list; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList<E> { | ||
private transient static int INITIAL_SIZE = 10; | ||
private transient int arrayLength; | ||
private transient int size; | ||
private transient E[] array; | ||
public ArrayList(){ | ||
array = (E[]) new Object[INITIAL_SIZE]; | ||
arrayLength = INITIAL_SIZE; | ||
} | ||
public ArrayList(int size){ | ||
if(size<=0){ | ||
throw new IllegalArgumentException("参数不可以小于0"); | ||
} | ||
array = (E[])new Object[size]; | ||
arrayLength = array.length; | ||
ensureCapacity(size); | ||
this.size = size; | ||
} | ||
public int size(){ | ||
return size; | ||
} | ||
public void add(E e){ | ||
ensureCapacity(size+1); | ||
array[size] = e; | ||
size++; | ||
} | ||
public E get(int index){ | ||
if(index<0 || index > size){ | ||
throw new IllegalArgumentException("索引越界"); | ||
} | ||
return array[index]; | ||
|
||
} | ||
public E set(int index, E e){ | ||
if(index<0 || index>size){ | ||
throw new IllegalArgumentException("索引越界"); | ||
} | ||
E result = array[index]; | ||
array[index] = e; | ||
return result; | ||
} | ||
private void ensureCapacity(int size){ | ||
E[] oldArray = array; | ||
int oldSize = arrayLength; | ||
while(size>arrayLength){ | ||
arrayLength = arrayLength + (arrayLength >> 1); | ||
array = Arrays.copyOf(oldArray, arrayLength); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
group05/1377699408/作业/20170226/src/test/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,59 @@ | ||
package test; | ||
|
||
import junit.framework.TestCase; | ||
import list.ArrayList; | ||
|
||
public class ArrayListTest extends TestCase { | ||
|
||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
} | ||
|
||
protected void tearDown() throws Exception { | ||
super.tearDown(); | ||
} | ||
|
||
public void testArrayList() { | ||
ArrayList<Integer> a = new ArrayList<Integer>(); | ||
assertEquals(a.size(), 0); | ||
} | ||
|
||
public void testArrayListInt() { | ||
ArrayList<Integer> a = new ArrayList<Integer>(9); | ||
assertEquals(a.size(), 9); | ||
} | ||
|
||
public void testSize() { | ||
ArrayList<Integer> a = new ArrayList<Integer>(9); | ||
assertEquals(a.size(), 9); | ||
ArrayList<Integer> a2 = new ArrayList<Integer>(100); | ||
assertEquals(a2.size(), 100); | ||
} | ||
|
||
public void testAdd() { | ||
ArrayList<Integer> a = new ArrayList<Integer>(); | ||
for (int i = 0; i < 1000; i++) { | ||
a.add(5); | ||
assertEquals(a.size(), i+1); | ||
assertEquals(a.get(i), new Integer(5)); | ||
} | ||
} | ||
|
||
public void testGet() { | ||
ArrayList<Integer> a = new ArrayList<Integer>(); | ||
a.add(6); | ||
assertEquals(a.get(0), new Integer(6)); | ||
|
||
} | ||
|
||
public void testSet() { | ||
ArrayList<Integer> a = new ArrayList<Integer>(); | ||
for (int i = 0; i < 100; i++) { | ||
a.add(56); | ||
} | ||
a.set(5, 66); | ||
assertEquals(a.get(5), new Integer(66)); | ||
assertEquals(a.get(7), new Integer(56)); | ||
} | ||
|
||
} |