写了一个简单的程序(ArrayList)

类别:Java 点击:0 评论:0 推荐:

/**
*ArrayList Test
*@author:fly29
*create on 2004-14-18
*/
import java.util.*;

public class Test
{
 public static void main(String[] args)
 {
    Collection TestSet=new ArrayList();
    String name="xingruyi";
    int age=22;
    Person p=new Person();
   
    //add Person object to ArrayList
    for(int i=1;i<5;i++)
    {
      p.setName(name);

      p.setAge(age);
      TestSet.add(p);
    }
   
    //list the ArrayList
    Iterator it=TestSet.iterator();
    while(it.hasNext())
    {
      Person p1=(Person)it.next();
      System.out.println("name: "+p1.getName());
      System.out.println("age: "+p1.getAge());
    }
 }
}

//class Person provide Setter and Getter methods
class Person
{
  private String name;
  private int age;

  Person()
  {
    super();
  }

  public void setAge(int a)
  {
    this.age=a;
  }

  public void setName(String n)
  {
    this.name=n;
  }

  public String getName()
  {
    return this.name;
  }

  public int getAge()
  {
    return this.age;
  }
}

本文地址:http://com.8s8s.com/it/it12477.htm