forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #8 from jacky1991/master
Work01 commit
- Loading branch information
Showing
7 changed files
with
501 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,19 @@ | ||
*.class | ||
*.classpath | ||
*.project | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
#ide config | ||
.metadata | ||
.recommenders | ||
/bin/ |
100 changes: 100 additions & 0 deletions
100
group09/1271620150/Work01/src/com/coding/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,100 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.ConcurrentModificationException; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private Object[] elements; | ||
|
||
private int size; | ||
|
||
public ArrayList(int initialCapacity) { | ||
super(); | ||
if (initialCapacity < 0) | ||
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); | ||
this.elements = new Object[initialCapacity]; | ||
} | ||
|
||
public ArrayList() { | ||
this(10); | ||
} | ||
|
||
public void add(Object obj) { | ||
ensureCapacity(size + 1); | ||
elements[size++] = obj; | ||
|
||
} | ||
|
||
public void add(int index, Object obj) { | ||
rangeCheck(index); | ||
ensureCapacity(size + 1); | ||
System.arraycopy(elements, index, elements, index + 1, size - index); | ||
elements[index] = obj; | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
rangeCheck(index); | ||
return elements[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
rangeCheck(index); | ||
Object toRemove = elements[index]; | ||
int numMoved = size - index - 1; | ||
if (numMoved > 0) | ||
System.arraycopy(elements, index + 1, elements, index, numMoved); | ||
elements[--size] = null; | ||
return toRemove; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private void ensureCapacity(int minCapacity) { | ||
int oldCapacity = elements.length; | ||
if (minCapacity > oldCapacity) { | ||
int newCapacity = oldCapacity * 2; | ||
if (newCapacity < minCapacity) | ||
newCapacity = minCapacity; | ||
elements = Arrays.copyOf(elements, newCapacity); | ||
} | ||
} | ||
|
||
private void rangeCheck(int index) { | ||
if (index >= size) | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
|
||
private String outOfBoundsMsg(int index) { | ||
return "Index: " + index + ", Size: " + this.size; | ||
} | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
|
||
private int pos = 0; | ||
|
||
public boolean hasNext() { | ||
return pos != size; | ||
} | ||
|
||
public Object next() { | ||
int i = pos; | ||
if (i >= size) | ||
throw new NoSuchElementException(); | ||
Object[] elements = ArrayList.this.elements; | ||
if (i >= elements.length) | ||
throw new ConcurrentModificationException(); | ||
pos = i + 1; | ||
return (Object) elements[i]; | ||
} | ||
} | ||
|
||
} |
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,7 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
Oops, something went wrong.