forked from diliuzuzhanghao/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from eloiseSJTU/master
Assignment 2 spec update
- Loading branch information
Showing
731 changed files
with
37,527 additions
and
284 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
62 changes: 0 additions & 62 deletions
62
coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java
This file was deleted.
Oops, something went wrong.
117 changes: 0 additions & 117 deletions
117
...ng2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/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,97 @@ | ||
package com.github.Ven13.coding2017.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
//返回集合大小 | ||
private int size = 0; | ||
|
||
//先给定一个长度为10的数组 | ||
Object[] elementData = new Object[100]; | ||
|
||
@Override | ||
//动态添加元素 | ||
public void add(Object o) { | ||
//先判断数组是否已满 | ||
if(size == elementData.length) { | ||
Object[] newObjects = new Object[elementData.length * 2]; | ||
System.arraycopy(elementData, 0, newObjects, 0, elementData.length); | ||
elementData = newObjects; | ||
} | ||
|
||
//为新添加的元素指定下标 | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
//先判断数组是否已满 | ||
if(size == elementData.length) { | ||
Object[] newObjects = elementData; | ||
this.elementData = new Object[elementData.length * 2]; | ||
for(int j = 0; j < newObjects.length; j++) { | ||
this.elementData[j] = newObjects[j]; | ||
} | ||
} | ||
|
||
for(int i = size - 1; i >= index; i--) { | ||
elementData[i+1] = elementData[i]; | ||
} | ||
|
||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index > size) { | ||
return null; | ||
}; | ||
|
||
int moveSize = size - index - 1; | ||
|
||
if (moveSize > 0) { | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
} | ||
elementData[--size] = null; | ||
|
||
//for(int i = index; i < elementData.length; i++) { | ||
// elementData[i] = elementData[i+1]; | ||
//} | ||
|
||
return elementData; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
|
||
private int currentIndex = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if(currentIndex >= size) return false; | ||
else return true; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object o = elementData[currentIndex]; | ||
currentIndex ++; | ||
return o; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.