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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 蒋昌宏 黑马帝   /  2012-3-11 23:58  /  1464 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.ListIterator;

  6. /**
  7. * 1、        定义一个Person对象,构造方法中定义私有属性name&age
  8. * 2、        创建多个Person对象
  9. * 3、        将多个Person对象存储到集合中,由于只做查询操作,因此采用ArrayList容器。
  10. * 4、        使用ListIterator从ArrayList中将中的元素取出并打印。
  11. * @author JiangChangHong
  12. *
  13. */
  14. class Person{
  15.         private String name;
  16.         private int age;
  17.         public Person(String name,int age){
  18.                 this.name = name;
  19.                 this.age = age;
  20.         }
  21.         public void show(){
  22.                 System.out.println("name="+name+"\t"+"age="+age);
  23.         }
  24. }
  25. public class PersonDemo {

  26.         /**
  27.          * @param args
  28.          */
  29.         public static void main(String[] args) {
  30.                 // TODO Auto-generated method stub
  31.                 Person p1 = new Person("Aaron",18);
  32.                 Person p2 = new Person("Abbott",19);
  33.                 Person p3 = new Person("Abel",20);
  34.                 Person p4 = new Person("Abner",21);
  35.                 Person p5 = new Person("Abraham",22);
  36.                 Person p6 = new Person("Adair",23);
  37.                 Person p7 = new Person("Adam",24);
  38.                 Person p8 = new Person("Addison",25);
  39.                 Person p9 = new Person("Adolph",26);
  40.                
  41.                 List co = new ArrayList();
  42.                
  43.                 co.add(p1);
  44.                 co.add(p2);
  45.                 co.add(p3);
  46.                 co.add(p4);
  47.                 co.add(p5);
  48.                 co.add(p6);
  49.                 co.add(p7);
  50.                 co.add(p8);
  51.                 co.add(p9);
  52.                
  53.                 //取出所有元素。迭代器.
  54.                 for(ListIterator it = co.listIterator(); it.hasNext(); ){
  55.                         System.out.println("next:"+it.next());
  56.                 }
  57.                
  58.                 //list集合特有的取出方式,通过角标遍历。
  59.                 for(int x=0; x<co.size(); x++){
  60.                         System.out.println("get:"+co.get(x));
  61.                 }
  62.         }
  63. }
复制代码
怎样才能打印出来不是内存地址。。。要实际name值和age值

2 个回复

倒序浏览
本帖最后由 了无尘 于 2012-3-12 02:35 编辑
  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.ListIterator;

  6. /**
  7. * 1、        定义一个Person对象,构造方法中定义私有属性name&age
  8. * 2、        创建多个Person对象
  9. * 3、        将多个Person对象存储到集合中,由于只做查询操作,因此采用ArrayList容器。
  10. * 4、        使用ListIterator从ArrayList中将中的元素取出并打印。
  11. * @author JiangChangHong
  12. *
  13. */
  14. class Person{
  15.         private String name;
  16.         private int age;
  17.         public Person(String name,int age){
  18.                 this.name = name;
  19.                 this.age = age;
  20.         }
  21.         public void show(){
  22.                 System.out.println("name="+name+"\t"+"age="+age);
  23.         }
  24.         
  25.         public String getShow()
  26.         {
  27.                 return "name="+name+"\t"+"age="+age;
  28.         }
  29. }
  30. public class PersonDemo {

  31.         /**
  32.          * @param args
  33.          */
  34.         public static void main(String[] args) {
  35.                 // TODO Auto-generated method stub
  36.                 Person p1 = new Person("Aaron",18);
  37.                 Person p2 = new Person("Abbott",19);
  38.                 Person p3 = new Person("Abel",20);
  39.                 Person p4 = new Person("Abner",21);
  40.                 Person p5 = new Person("Abraham",22);
  41.                 Person p6 = new Person("Adair",23);
  42.                 Person p7 = new Person("Adam",24);
  43.                 Person p8 = new Person("Addison",25);
  44.                 Person p9 = new Person("Adolph",26);
  45.                
  46.                 List co = new ArrayList();
  47.                
  48.                 co.add(p1);
  49.                 co.add(p2);
  50.                 co.add(p3);
  51.                 co.add(p4);
  52.                 co.add(p5);
  53.                 co.add(p6);
  54.                 co.add(p7);
  55.                 co.add(p8);
  56.                 co.add(p9);
  57.                
  58.                 //取出所有元素。迭代器.
  59.                 for(ListIterator it = co.listIterator(); it.hasNext(); ){
  60.                         System.out.println("next:"+((Person)it.next()).getShow());
  61.                 }
  62.                
  63.                 //list集合特有的取出方式,通过角标遍历。
  64.                 for(int x=0; x<co.size(); x++){
  65.                         System.out.println("get:"+((Person)co.get(x)).getShow());
  66.                 }
  67.         }
  68. }
复制代码
当然为了简单点,我们一般都是重写Object类继承来的toString()方法,这样就不需要在强制转换了
回复 使用道具 举报
  1. for(ListIterator it = co.listIterator(); it.hasNext(); ){
  2.                         System.out.println("next:"+it.next());  //ti.next()是个对象
  3.                 }
  4.                
  5.                 //list集合特有的取出方式,通过角标遍历。
  6.                 for(int x=0; x<co.size(); x++){
  7.                         System.out.println("get:"+co.get(x));//co.get(x)也是对象
复制代码
存进去的是person对象,所以迭代出来的也是对象,所以迭代完了要调用show()方法才能打印出来。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马