import java.util.*;public class rq {
public static void main(String[] args) {
Collection<Integer> text = new ArrayList<Integer>();
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = i;
text.add(a[i]);
}

Iterator m = text.iterator();
int count=0;
while (m.hasNext()) {
count++;
 if(count==4){
 System.out.println(a[count]);
 System.out.println(m.next());//我要打印第5个数,为什么用迭代器打印出来的值是0呢
 break;
 } 
}
}
}

解决方案 »

  1.   

    因为你在while中一直判断m.hasNext(),但是 if(count==4){
    中第一次使用m.next()
      

  2.   

    到count==4的时候才第一次调m.next();当然是0了
    加上红色部分就可以了import java.util.*;public class rq {
        public static void main(String[] args) {
            Collection<Integer> text = new ArrayList<Integer>();
            int[] a = new int[10];
            for (int i = 0; i < a.length; i++) {
                a[i] = i;
                text.add(a[i]);
            }
            
            Iterator m = text.iterator();
            int count=0;
            while (m.hasNext()) {
                count++;
                m.next();
                 if(count==4){
                     System.out.println(a[count]);
                     System.out.println(m.next());//我要打印第5个数,为什么用迭代器打印出来的值是0呢
                     break;
                 } 
            }
        }
    }
      

  3.   

    红色没出来,就是下面这句
    m.next();
      

  4.   

    看清楚点,在count == 4之前m.next()执行了吗?
      

  5.   

    只有当count等于4的时候才会取出第一个元素
      

  6.   


    import java.util.*;
    public class Test {
        public static void main(String[] args) {
            Collection<Integer> text = new ArrayList<Integer>();
            int[] a = new int[10];
            for (int i = 0; i < a.length; i++) {
                a[i] = i;
                text.add(a[i]);
            }      
            Iterator m = text.iterator(); 
                 for(int count=0;count<=4;count++){
                     System.out.println(a[count]);
                     System.out.println(m.next());         
            }
        }
    } 这个代码是你要的结果吧,知道执行次数的最好用for,
    而且在你代码里面 count==4是不对的,应该是count<=4,并且m.next执行了二次,