import java.util.Iterator;
import java.util.ArrayList;
public class work08 { public static void main(String args[])
{
ArrayList<car> a=new ArrayList<car>();
car car1=new car("奔驰","5","1000");
car car2=new car("别克","5","1023");
car car3=new car("通用","4","2000");
car car4=new car("沃尔沃","5","1004");
car car5=new car("劳斯莱斯","6","999");
//car1("奔驰","5","1000");

a.add(car1);
a.add(car2);
a.add(car3);
a.add(car4);
a.add(car5);
Iterator<car> it=a.iterator();
 while(it.hasNext()) 
{

System.out.print(" "+ it.next().getName()+" "+ it.next().getNumber()+" "+ it.next().getOutput());

}}}
class car
{
private String name;
private String number;
private String output;
public car(String a,String b,String c) {
// TODO Auto-generated constructor stub
setName(a);
setNumber(b);
setOutput(c);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output = output;

}
}

解决方案 »

  1.   

    while(it.hasNext()) {
    //下面2行为正确代码
    Car car=it.next();
    System.out.print(" "+car.getName()+" "+ car.next().getNumber()+" "+ car.next().getOutput());
    //System.out.print(" "+ it.next().getName()+" "+ it.next().getNumber()+" "+ it.next().getOutput());你原来的代码判断一次后
    连读了三次next()将直接使指针跳3格了

    }
    }图解如下,本来是
                 point -->         0
      1
      2     
      3
      4
      5
      6
      
    你的代码运行一次while(it.hasNext()){代码}以后那个point指针直接跳转到第4个car去了
                                      0
      1
      2     
    point -->   3
      4
      5
      6
      

  2.   

    打错代码了,重发while(it.hasNext()) {
    //下面2行为正确代码
    Car car=it.next();
    System.out.print(" "+car.getName()+" "+ car.getNumber()+" "+ car.getOutput());
    //System.out.print(" "+ it.next().getName()+" "+ it.next().getNumber()+" "+ it.next().getOutput());你原来的代码判断一次后
    连读了三次next()将直接使指针跳3格了

    }
    }/*图解如下,本来是
                 point -->         0
      1
      2     
      3
      4
      5
      6
      
    你的代码运行一次while(it.hasNext()){代码}以后那个point指针直接跳转到第4个car去了
                                      0
      1
      2     
    point -->   3
      4
      5
      6
      */
      

  3.   

    我试了一下,发现是因为System.out.print(" "+ it.next().getName()+" "+ it.next().getNumber()+" "+ it.next().getOutput());这句有问题。
    先类型转换一下car c = it.next();
    然后这样写就不出错了System.out.print(" "+ c.getName()+" "+ c.getNumber()+" "+ c.getOutput());
      

  4.   

    LZ可以结贴了~~~
    it.next()将索引给搞到下面去了
      

  5.   

    修改后不会死循环么?我试了下死循环,而我只是添加了car6数据就正常了,应该是while里面的语句要抛出异常吧?疑惑中。
      

  6.   

    while(it.hasNext())  
    {
    car car=it.next();
    System.out.println(" "+car.getName()+" "+ car.getNumber()+" "+ car.getOutput());
    }
    你next()一次指针向下走一次  我可以先取出来 car car=it.next();
    试试看
      

  7.   

    锅,为什么要先取出来car c = it.next()这个啊,以前用的时候好像也没怎么加这句,球解释啊
      

  8.   

    我改成增强的for循环就是可以的,鸭梨啊
    for(car c : a){
    system.out.println(c.getName());
    }
    LZ,最好改写car的toString方法,然后直接system.out.println(c)就行了