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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王虎 中级黑马   /  2012-10-13 13:27  /  1240 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王虎 于 2012-10-13 13:29 编辑

上午老师留了一个小题:用集合存放三个Student对象,并用迭代器遍历Student,(Student对象里包含name sex age)

为此自己写了四种方法
写法一:--------------------------------------------------------
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
class Test5
{
public static void main(String[] args)
{
  Collection c = new ArrayList();
  Student s1 = new Student();
  s1.name="张三";
  s1.sex="男";
  s1.age=23;
  Student s2 = new Student();
  s2.name="李四";
  s2.sex="男";
  s2.age=24;
  Student s3 = new Student();
  s3.name="王五";
  s3.sex="男";
  s3.age=25;
  
  c.add(s1);
  c.add(s2);
  c.add(s3);
  Iterator it =  c.iterator();
  while(it.hasNext())
  {
   Student s = (Student)it.next();
   System.out.println(s);
  }
}
}
class Student
{
  String name;
  String sex;
  int age;
public String toString()
{
  return name+" "+sex+" "+age;
}
}

写法二 ------------------------------------------------------------
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
class Test5
{
public static void main(String[] args)
{
  Collection c = new ArrayList();
  Student s1 = new Student();
  s1.name="张三";
  s1.sex="男";
  s1.age=23;
  Student s2 = new Student();
  s2.name="李四";
  s2.sex="男";
  s2.age=24;
  Student s3 = new Student();
  s3.name="王五";
  s3.sex="男";
  s3.age=25;
  
  c.add(s1);
  c.add(s2);
  c.add(s3);
  Iterator it =  c.iterator();
  while(it.hasNext())
  {
   Object o = it.next();
   System.out.println(o);
  }
}
}
class Student
{
  String name;
  String sex;
  int age;
public String toString()
{
  return name+" "+sex+" "+age;
}
}

方法三:------------------------------------------------------
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
class Test5
{
public static void main(String[] args)
{
  Collection c = new ArrayList();
  Student s1 = new Student("张三","男",23);
  Student s2 = new Student("李四","男",24);
  Student s3 = new Student("王五","男",25);
  
  c.add(s1);
  c.add(s2);
  c.add(s3);
  Iterator it =  c.iterator();
  while(it.hasNext())
  {
   Object o = it.next();
   System.out.println(o);
  }
}
}
class Student
{
  private String name;
  private String sex;
  private int age;
public Student(String name,String sex,int age)
{
  this.name = name;
  this.sex=sex;
  this.age=age;
}
public String toString()
{
  return name+" "+sex+" "+age;
}
}
方法四:--------------------------------------------------------
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
class Test5
{
public static void main(String[] args)
{
  Collection c = new ArrayList();
  Student s1 = new Student();
  s1.setName("张三");
  s1.setSex("男");
  s1.setAge(23);
  Student s2 = new Student();
  s2.setName("李四");
  s2.setSex("男");
  s2.setAge(24);
  
  Student s3 = new Student();
  s3.setName("王五");
  s3.setSex("男");
  s3.setAge(25);
  
  c.add(s1);
  c.add(s2);
  c.add(s3);
  Iterator it =  c.iterator();
  while(it.hasNext())
  {
   Object o = it.next();
   System.out.println(o);
  }
}
}
class Student
{
  private String name;
  private String sex;
  private int age;

public Student()
{
}

public void setName(String name)
{
  this.name= name;
}
public void setSex(String sex)
{
  this.sex=sex;
}
public void setAge(int age)
{
  this.age=age;
}

public String getName()
{
  return this.name;
}
public String getSex()
{
  return this.sex;
}
public int getAge()
{
  return this.age;
}
public String toString()
{
  return name+" "+sex+" "+age;
}
}

写完之后感觉唯一感觉:编程无定法~

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马