Topic: 数组问题

  Print this page

1.数组问题 Copy to clipboard
Posted by: hunter1206
Posted on: 2008-02-04 16:21

interface Selector {
boolean end();
Object current();
void next();
}

public class Sequence {
private static Test monitor = new Test();
private Object[] objects;
private int next = 0;
public Sequence(int size) { objects = new Object[size]; }
public void add(Object x) {
if(next < objects.length)
objects[next++] = x;
}
private class SSelector implements Selector {
private int i = 0;
public boolean end() { return i == objects.length; }
public Object current() { return objects; }
public void next() { if(i < objects.length) i++; }
}
public Selector getSelector() { return new SSelector(); }
public static void main(String[] args) {
Sequence sequence = new Sequence(10);
for(int i = 0; i < 10; i++)
sequence.add(Integer.toString( i ) );
Selector selector = sequence.getSelector();
while(!selector.end()) {
System.out.println(selector.current());
selector.next();
}
}
} ///:~

这里while里面的判断是 当数组的length属性为0的时候 数组到了末尾?

谁能帮忙解释下这个while表达式.

2.Re:数组问题 [Re: hunter1206] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2008-02-16 14:36

SSelector中的i是下一个访问到的元素的位置索引,每访问过一个数组的元素后 调用next()都会i++
如果 return i == objects.length 为true,表明数组的最后一个元素已经访问过了,同时i会自增,i就等于objects.length了
!end()为真,就是说数组还有元素没有被访问时候执行while循环


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923