u010581514 于 2015.07.14 07:48 提问

java迭代问题，二次迭代问题i

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyArrrayList implements Iterable {
private static final int DEFAULT_CAPACITY = 10;
private int theSize;
private AnyType[] theItems;

``````public MyArrrayList() {
clear();
}

public void clear() {
theSize = 0;
ensureCapality(DEFAULT_CAPACITY);
}

public int size() {
return theSize;
}

public boolean isEmpty() {
return size() == 0;
}

public void trimSize() {
ensureCapality(size());
}

public AnyType get(int idx) {
if (idx < 0 || idx >= size())
throw new ArrayIndexOutOfBoundsException();
return theItems[idx];
}

public AnyType set(int idx, AnyType newVal) {
if (idx < 0 || idx >= size())
throw new ArrayIndexOutOfBoundsException();
AnyType old = theItems[idx];
theItems[idx] = newVal;
return old;
}

@SuppressWarnings("unchecked")
public void ensureCapality(int newCapality) {
if (newCapality < theSize)
return;
AnyType[] old = theItems;
theItems = (AnyType[]) new Object[newCapality];
for (int i = 0; i < size(); i++) {
theItems[i] = old[i];
}
}

public boolean add(AnyType x) {
return true;
}

public void add(int idx, AnyType x) {
if (theItems.length == size())
ensureCapality(size() * 2 + 1);
for (int i = theSize; i > idx; i--)
theItems[i] = theItems[i - 1];
theItems[idx] = x;
theSize++;
}

public AnyType remove(int idx) {
if (idx < 0 || idx >= size())
throw new ArrayIndexOutOfBoundsException();
AnyType removeItem = theItems[idx];
for (int i = idx; i < theSize; i++)
theItems[i] = theItems[i + 1];
theSize--;
return removeItem;
}

@Override
public Iterator<AnyType> iterator() {
// TODO Auto-generated method stub
return new ArrayListIterator(this);
}

private static class ArrayListIterator<AnyType> implements Iterator<AnyType> {
private int current = 0;
private MyArrrayList<AnyType> theList;
public ArrayListIterator(MyArrrayList<AnyType> list){
theList=list;
}

public boolean hasNext() {
return current < theList.size();
}

public AnyType next() {

return theList.theItems[current++];
}

public void remove() {
theList.remove(--current);
}
}

public static void main(String[] args) {
MyArrrayList<Integer> list=new MyArrrayList<Integer>();
for(int i=1;i<20;i++)

Iterator<Integer> it=list.iterator();
while(it.hasNext())
{
System.out.print(it.next());
System.out.print(" ");

}
Iterator<Integer> its=list.iterator();

while(it.hasNext())
{
System.out.println(it.next());
}

}
``````

}

7个回答

strutce      2015.07.14 08:58

Iterator its=list.iterator();

``````while(it.hasNext())
{
System.out.println(it.next());
}
``````

u010581514 对啦！谢谢！！就是我其他没错是不是啊？
2 年多之前 回复
strutce 还有 its.next() 这2个
2 年多之前 回复
richper_6   2015.07.14 08:09

hasnext 已经指向最后一个了下一个自然不满足条件

jcc741486706 有没有办法改进
2 年多之前 回复
u012216727      2015.07.14 08:22

jcc741486706 不会改～=_=不知道怎么改
2 年多之前 回复
lingfeiwen   2015.07.14 09:38

zmbc48   2015.07.14 09:44

it.next();

it =list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}

zmbc48   2015.07.14 09:44

it.next();

it =list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}

ujnlvxin   2015.07.14 21:18

it,its。没有区分，接着对it取hasnext肯定是假啊