public class Test2{
public static void main(String[] args){
LinkedList1 ll=new LinkedList1();
for(int i=0;i<25;i++){
ll.add(new Cat(i));
}
while(ll.hasNext()){
System.out.println(ll.next());
}
while(ll.hasNext()){
System.out.println(ll.next());
}
}
}
class LinkedList1{
private Node n;
private Node nn;
private Node head;
private Node tell;
private int index;
private int currentIndex=0;
private Object o;
public void add(Object o){
n=new Node(o,null);
if(head==null){
head=n;
tell=n;
}
tell.setNode(n);
tell=n;
index++;
}
public int size(){
return index;
}
public boolean hasNext(){
if(currentIndex>=index){
currentIndex=0;
return false;
}
else{
return true;
}
}
public Object next(){
o=head.getObject();
head=head.getNode();
currentIndex++;
return o;
}
}
class Node{
private Object o;
private Node n;
Node(Object o,Node n){
this.o=o;
this.n=n;
}
public Object getObject(){
return o;
}
public Node getNode(){
return n;
}
public void setObject(Object o){
this.o=o;
}
public void setNode(Node n){
this.n=n;
}
}
class Cat{
private int id;
Cat(int id){
this.id=id;
}
public int getId(){
return id;
}
public String toString(){
return "Cat id "+id+" is couStruct";
}
}
今天写了个LinkedList类 但是只能遍历一遍,第二遍就会出错。。
我知道我在遍历的时候改变了原来的数据 所有才有这样的结果
请教各位高手, 有没有好一点的方法 实现遍历呢。