服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

/**
*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;
  }
}

扫描关注微信公众号