ArrlyListコレクション
4114 ワード
public class Person
{
private String name;//,sex;
private int age;
public Person(String name, int age)
{
super();
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
名前年齢性別身長体重などのデータを含むPersonクラスを定義し、対応するgetとsetメソッドを提供します.
import java.util.ArrayList;
import java.util.Iterator;
public class Test
{
public static void main(String[] args)
{
Person pA = new Person("A", 20);
Person pB = new Person("B", 21);
Person pC = new Person("C", 22);
Person pD = new Person("D", 24);
ArrayList<Person> al = new ArrayList<Person>();
al.add(pA);
al.add(pB);
al.add(pC);
al.add(pD);
for (Iterator<Person> iterator = al.iterator(); iterator.hasNext();) //
{
Person p = (Person) iterator.next(); // person
System.out.println(p.getName()+" "+p.getAge());
}
}
}