Skip to content

Commit

Permalink
Work01 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jacky1991 committed Feb 26, 2017
1 parent 81a4a39 commit 011550b
Show file tree
Hide file tree
Showing 7 changed files with 501 additions and 0 deletions.
19 changes: 19 additions & 0 deletions group09/1271620150/Work01/.gitignore
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 group09/1271620150/Work01/src/com/coding/basic/ArrayList.java
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];
}
}

}
7 changes: 7 additions & 0 deletions group09/1271620150/Work01/src/com/coding/basic/Iterator.java
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();

}
Loading

0 comments on commit 011550b

Please sign in to comment.