Test类
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List <Users> list = new ArrayList<Users>();
for(int i = 0;i<10;i++){
Users user = new Users();
list.add(user);
}
for(int i = 0;i<list.size();i++){
System.out.println("请输入姓名:");
String name = sc.next();
list.get(i).setName(name);
System.out.println("请输入密码:");
String pwd = sc.next();
list.get(i).setPwd(pwd);
System.out.println("姓名:"+list.get(i).getName()+" 密码:"+list.get(i).getPwd());
}
}
}
users类
public class Users {
private String Name;
private String pwd;
public Users() {}
public Users(String name, String pwd) {
this.Name = name;
this.pwd = pwd;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
思想:实质上是使用连续创建对象再将其放入集合之中,使用list.add方法。