WEB开发网
开发学院WEB开发Jsp 写了一个简单的程序(ArrayList) 阅读

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

 2008-01-05 18:36:34 来源:WEB开发网   
核心提示:/***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 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;
  }
}


Tags:一个 简单 程序

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接