A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 高会仁 中级黑马   /  2012-12-7 11:45  /  1319 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.ArrayList;
  2. import java.util.Iterator;

  3. class Person{
  4.         private String name;
  5.         private int age;
  6.         public Person(String name,int age){
  7.                 this.name = name;
  8.                 this.age = age;
  9.         }
  10.         public String getName() {
  11.                 return name;
  12.         }
  13.         public int getAge() {
  14.                 return age;
  15.         }
  16.        
  17. }
  18. public class ArrayListTest {

  19.         /**
  20.          * @param args
  21.          */
  22.         public static void sop(Object obj){
  23.                 System.out.println(obj);
  24.         }
  25.         public static void main(String[] args) {

  26.                 ArrayList al = new ArrayList();
  27.                 al.add(new Person("a",1));
  28.                 al.add(new Person("b",2));
  29.                 al.add(new Person("c",3));
  30.                 al.add(new Person("d",4));
  31.                
  32.                 Iterator<Person> it = al.iterator();
  33.                
  34.                 while(it.hasNext()){
  35.                        
  36.                         sop(it.next().getName()+"----"+it.next().getAge());
  37.                 }
  38.         }
复制代码
为什么打印结果是:
a----2
c----4呢?为什么不是a---1  b---2  c---3 d---4这样?

点评

在循环中调用next()一次,就要hasNext()判断一次 。  发表于 2012-12-7 23:22

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

7 个回复

倒序浏览
一次循环中执行了两次it.next()
回复 使用道具 举报
执行了两次it.next,第一次取到a,然后下一次取到了b对应的那个2。
改成
Person p = it.next();
sop(p.getName()+"----"+p.getAge());
就应该好了

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
本帖最后由 张向辉 于 2012-12-7 14:29 编辑

每执行一次it.next(); it就移到下个数据指向。
sop(it.next().getName()+"----"+it.next().getAge());其中第一个it.next()指的是new Person("a",1)的对象;第二个指的是new Person("b",2)的对象;所以此句打印的是a----2
这句如果改上  sop(it.next().getName()+"----"+it.next().getName()+"----"+it.next().getName()+"----"+it.next().getName());
打印a----b----c----d



评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
本帖最后由 吴愿涛 于 2012-12-7 22:13 编辑
  1. package com.itheima.bbs.exercise;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;

  4. class Person{
  5.         private String name;
  6.         private int age;
  7.         public Person(String name,int age){
  8.                 this.name = name;
  9.                 this.age = age;
  10.         }
  11.         public String getName() {
  12.                 return name;
  13.         }
  14.         public int getAge() {
  15.                 return age;
  16.         }
  17.         
  18. }
  19. class ArrayListTest {

  20.         /**
  21.          * @param args
  22.          */
  23.         public static void sop(Object obj){
  24.                 System.out.println(obj);
  25.         }
  26.         public static void main(String[] args) {

  27.                 ArrayList al = new ArrayList();
  28.                 al.add(new Person("a",1));
  29.                 al.add(new Person("b",2));
  30.                 al.add(new Person("c",3));
  31.                 al.add(new Person("d",4));
  32.                 al.add(new Person("e",5));
  33.                 al.add(new Person("f",6));
  34.                
  35.                 Iterator<Person> it = al.iterator();
  36.                
  37.                 while(it.hasNext()){
  38.                                 Person p = it.next();
  39.                                 String  name = p.getName();
  40.                                 int age = p.getAge();
  41.                                 
  42.                         sop(name+":"+age);
  43.                 }
  44.         }
  45. }
复制代码
回复 使用道具 举报
那当然不是你想要的答案呀!你在打印语句中写错了,你把它改成
Person p = it.next();
System.out.println((p.getName() + "----" + p.getAge()));
就行了
  1. public class ArrayListTest {
  2.         public static void main(String[] args) {
  3.                 ArrayList<Person> al = new ArrayList<Person>();
  4.                 al.add(new Person("a",1));
  5.                 al.add(new Person("b",2));
  6.                 al.add(new Person("c",3));
  7.                 al.add(new Person("d",4));
  8.                
  9.                 Iterator<Person> it = al.iterator();
  10.                
  11.                 while(it.hasNext()){
  12.                         Person p = it.next();
  13.                         System.out.println((p.getName() + "----" + p.getAge()));
  14.                 }
  15.         }
  16. }
复制代码
回复 使用道具 举报
吴愿涛 发表于 2012-12-7 22:10

记住!!!在循环中调用next()一次,就要hasNext()判断一次 。
回复 使用道具 举报
谢谢各位大神。懂了{:soso_e113:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马