import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/*
键盘录入姓名和年龄存储ArrayList中
如果用户输出over,结束输入,迭代ArrayList
*/
public class Test4 {
public static void main(String[] args) {
ArrayList<Person> array = new ArrayList<Person>();
System.out.println("请您录入信息,输入over结束!");
Scanner scs = new Scanner(System.in);
String s= scs.nextLine();// lisi 30
while(!"over".equals(s)){
String[] str = s.split(" ");
array.add(new Person(str[0],Integer.parseInt(str[1])));
/* System.out.println("请您继续录入:");
Scanner sci = new Scanner(System.in);
int i= sci.nextInt();
array.add(new Person(s,i));
System.out.println("请您录入信息,输入over结束!");*/
s = scs.nextLine();
}
Iterator<Person> it = array.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Person{
private String name;
private int age;
Person(){}
Person(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public String toString(){
return name+".."+age;
}
} |
|