求这段java代码的详细注释 要每行的注释
interfaceSelector{booleanend();Objectcurrent();voidnext();}publicclassSequence{privat...
interface Selector {
boolean end();
Object current();
void next();
}
public class Sequence {
private Object[] items;
private int next = 0;
public Sequence(int size) { items = new Object[size]; }
public void add(Object x) {
if(next < items.length)
items[next++] = x;
}
private class SequenceSelector implements Selector {
private int i = 0;
public boolean end() { return i == items.length; }
public Object current() { return items[i]; }
public void next() { if(i < items.length) i++; }
}
public Selector selector() {
return new SequenceSelector();
}
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.selector();
while(!selector.end()) {
System.out.print(selector.current() + " ");
selector.next();
}
}
} /* Output:
0 1 2 3 4 5 6 7 8 9
*///:~ 展开
boolean end();
Object current();
void next();
}
public class Sequence {
private Object[] items;
private int next = 0;
public Sequence(int size) { items = new Object[size]; }
public void add(Object x) {
if(next < items.length)
items[next++] = x;
}
private class SequenceSelector implements Selector {
private int i = 0;
public boolean end() { return i == items.length; }
public Object current() { return items[i]; }
public void next() { if(i < items.length) i++; }
}
public Selector selector() {
return new SequenceSelector();
}
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.selector();
while(!selector.end()) {
System.out.print(selector.current() + " ");
selector.next();
}
}
} /* Output:
0 1 2 3 4 5 6 7 8 9
*///:~ 展开
1个回答
展开全部
//选择器(接口)
interface Selector {
//是否结束
boolean end();
//得到当前对象
Object current();
//下一个对象
void next();
}
//序列类
public class Sequence {
//对象数组
private Object[] items;
//当前索引为0
private int next = 0;
//设置数组大小
public Sequence(int size) {
items = new Object[size];
}
//向数组中加入一个对象
public void add(Object x) {
//如果当前索引小于数组的长度,就向数组中加入一个对象
if (next < items.length)
items[next++] = x;
}
//一个实现选择器接口的类
private class SequenceSelector implements Selector {
private int i = 0;
public boolean end() {
//返回i是否等于对象数组的长度,如果等于,说明结束,否则说明未结束
return i == items.length;
}
public Object current() {
//返回对象数组中索引为i的对象
return items[i];
}
public void next() {
//i小于对象数组的长度,i自加1
if (i < items.length)
i++;
}
}
//得到一个选择器对象
public Selector selector() {
return new SequenceSelector();
}
//main方法
public static void main(String[] args) {
//实现化序列类,序列类中的对象数组有10个对象
Sequence sequence = new Sequence(10);
//通过循环向对象数组中插入0,1,2,3,4,5,6,7,8,9对象。对象类型为String
for (int i = 0; i < 10; i++)
sequence.add(Integer.toString(i));
//实例化选择器
Selector selector = sequence.selector();
//while循环,当选择器判断为结尾时,跳出循环
while (!selector.end()) {
//输出当前对象
System.out.print(selector.current() + " ");
//将选择器索引加1
selector.next();
}
}
}
interface Selector {
//是否结束
boolean end();
//得到当前对象
Object current();
//下一个对象
void next();
}
//序列类
public class Sequence {
//对象数组
private Object[] items;
//当前索引为0
private int next = 0;
//设置数组大小
public Sequence(int size) {
items = new Object[size];
}
//向数组中加入一个对象
public void add(Object x) {
//如果当前索引小于数组的长度,就向数组中加入一个对象
if (next < items.length)
items[next++] = x;
}
//一个实现选择器接口的类
private class SequenceSelector implements Selector {
private int i = 0;
public boolean end() {
//返回i是否等于对象数组的长度,如果等于,说明结束,否则说明未结束
return i == items.length;
}
public Object current() {
//返回对象数组中索引为i的对象
return items[i];
}
public void next() {
//i小于对象数组的长度,i自加1
if (i < items.length)
i++;
}
}
//得到一个选择器对象
public Selector selector() {
return new SequenceSelector();
}
//main方法
public static void main(String[] args) {
//实现化序列类,序列类中的对象数组有10个对象
Sequence sequence = new Sequence(10);
//通过循环向对象数组中插入0,1,2,3,4,5,6,7,8,9对象。对象类型为String
for (int i = 0; i < 10; i++)
sequence.add(Integer.toString(i));
//实例化选择器
Selector selector = sequence.selector();
//while循环,当选择器判断为结尾时,跳出循环
while (!selector.end()) {
//输出当前对象
System.out.print(selector.current() + " ");
//将选择器索引加1
selector.next();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询