Skip to content

Commit

Permalink
Merge pull request #12 from Hipple/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
haolipeng authored Feb 27, 2017
2 parents f8a1eb7 + d5a0650 commit 91eeb6e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
1 change: 0 additions & 1 deletion group18/1159828430/20170219/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
40 changes: 39 additions & 1 deletion group18/1159828430/20170219/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ public boolean isEmpty(){
return size == 0;
}

//迭代器
public Iterator iterator(){
return null;
return new ArrayListIterator(this);
}

//动态增加ArrayList大小
Expand Down Expand Up @@ -128,6 +129,35 @@ private void fastRemove(int index) {
}
elementData[--size] = null;
}

private class ArrayListIterator implements Iterator{
private ArrayList list = null;
private int cursor = 0;
private int lastRet = -1;

public ArrayListIterator(ArrayList list){
this.list = list;
}
@Override
public boolean hasNext() {
return cursor != list.size;
}

@Override
public Object next() {
lastRet = cursor;
Object o = list.get(lastRet);
cursor ++;
return o;
}
@Override
public void remove() {
list.remove(lastRet);
cursor = lastRet;
lastRet = -1;
}

}

}
class testArrayList{
Expand All @@ -138,8 +168,16 @@ public static void main(String[] args) {
}
arrayList.add(5,15);
arrayList.remove(11);
Iterator it = arrayList.iterator();
while(it.hasNext()) {
Integer o = (Integer)it.next();
if(o == 8){
it.remove();
}
}
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("value is "+arrayList.get(i));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
public interface Iterator {
public boolean hasNext();
public Object next();
public void remove();

}
38 changes: 37 additions & 1 deletion group18/1159828430/20170219/src/com/coding/basic/LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coding.basic;

import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;

/**
Expand Down Expand Up @@ -88,7 +89,7 @@ public Object getFirst() {
}

public Iterator iterator(){
return null;
return new LinkedListIterator();
}

//头部增加节点
Expand Down Expand Up @@ -225,6 +226,41 @@ private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}

//迭代器
private class LinkedListIterator implements Iterator{
private Node lastReturned = null;
private Node next;
private int nextIndex;

public boolean hasNext() {
return nextIndex < size;
}

public Object next() {
if (!hasNext())
throw new NoSuchElementException();

lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.data;
}

public void remove() {
if (lastReturned == null)
throw new IllegalStateException();

Node lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
}
}

//节点对象
private static class Node{
Object data;
Node next;
Expand Down

0 comments on commit 91eeb6e

Please sign in to comment.