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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 纷飞尽 中级黑马   /  2013-12-25 21:27  /  1569 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有一个person类:package com.Collection.ArrayList;

public class Person {
  private String name;
  private int age;
  public Person(){}
  public Person(String name,int age){this.name=name;this.age=age;}
public String getName() {
        return name;
}
public void setName(String name) {
        this.name = name;
}
public int getAge() {
        return age;
}
public void setAge(int age) {
        this.age = age;
}

}

一个ArrayTest类如下,
package com.Collection.ArrayList;
import java.util.*;
public class ArrayDemo {

        public static void main(String[] args) {
               
                        demo();
        }
    public static void demo()
    {
           
                ArrayList al = new ArrayList<Person>();
                al.add(new Person("张三",55));
                al.add(new Person("李四",15));
                al.add(new Person("王武",65));
                al.add(new Person("王武",65));
       
                Iterator<Person>  it =al.iterator();
                while(it.hasNext())
                {
                       
                        System.out.println(it.next().getName()+it.next().getAge());
                }
           
    }
}
为什么遍历结果只是输出啦:张三15 王武65两个人,老毕讲过,忘记啦!

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

6 个回复

倒序浏览
你那个it.next()用了两次,你定义一个对象接收
回复 使用道具 举报
你对next方法还没有深入的理解,next方法,不仅取出了元素,
还把底层指向元素的指针指向了后面一个元素,当你使用
第一个it.next().getName()打印的是第一个人的名字,
第一个it.next().getAge()打印的是第二个人的年龄,
第二个it.next().getName()打印的是第三个人的名字,
第二个it.next().getAge()打印的是第四个人的年龄,
再hasNext判断就没有了,就结束循环了。

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
while循环中修改如下:
  1. while(it.hasNext())
  2.                 {
  3.                         Object obj = it.next();
  4.                         Person person = (Person) obj;
  5.                         System.out.println(person.getName()+person.getAge());
  6.                 }
复制代码
你的你面System.out.println(it.next().getName()+it.next().getAge());
这一句话里面使用了两个it.next(),第一个就是获取到了“张三”的名字了,第二个的话就是另一个person了也就是获取到了“李四”的年龄15;
后面的也是如此。


评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
  1. public class Person {
  2.   private String name;
  3.   private int age;
  4.   public Person(){}
  5.   public Person(String name,int age){this.name=name;this.age=age;}
  6. public String getName() {
  7.         return name;
  8. }
  9. public void setName(String name) {
  10.         this.name = name;
  11. }
  12. public int getAge() {
  13.         return age;
  14. }
  15. public void setAge(int age) {
  16.         this.age = age;
  17. }

  18. }

  19. 一个ArrayTest类如下,
  20. package com.Collection.ArrayList;
  21. import java.util.*;
  22. public class ArrayDemo {

  23.         public static void main(String[] args) {
  24.                
  25.                         demo();
  26.         }
  27.     public static void demo()
  28.     {
  29.            
  30.                 ArrayList al = new ArrayList<Person>();
  31.                 al.add(new Person("张三",55));
  32.                 al.add(new Person("李四",15));
  33.                 al.add(new Person("王武",65));
  34.                 al.add(new Person("王武",65));
  35.       
  36.                 Iterator<Person>  it =al.iterator();
  37.                 while(it.hasNext())
  38.                 {
  39.                        
  40.                          Person p= it.next();
  41.                         System.out.println(p.getName()+p.getAge());
  42.                 }
  43.            
  44.     }
  45. }
复制代码


代码修改如上所示,因为你在输出的时候it.next()是指向了两次了,所以输出的都不对应以及没有遍历完

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
  1. public static void demo() {

  2.                 ArrayList<Person> al = new ArrayList<Person>();
  3.                 al.add(new Person("张三", 55));
  4.                 al.add(new Person("李四", 15));
  5.                 al.add(new Person("王武", 65));
  6.                 al.add(new Person("王武", 65));

  7.                 Iterator<Person> it = al.iterator();
  8.                 while (it.hasNext()) {
  9.                         Person p = it.next();
  10.                         System.out.println(p.getName() + p.getAge());
  11.                 }

  12.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
很简单的啊
你的输出组成是这样的张三.name +李四.age +王武.name + 王武.age
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马