將User物件加入到ArrayList中,並且遍歷輸出
阿新 • • 發佈:2019-01-25
package day14;
import java.util.*;
@Override
public String toString() {
return "User [name=" + name + ", passWord=" + passWord + "]";
}
public class TestUser {
public static void main(String[] args) {
List<User> list = new ArrayList<User>();
Scanner in = new Scanner(System.in);
for(int i=0;i<3;i++){
System.out.print("輸入物件的名字:");
String name =in.next();
System.out.print("輸入物件的密碼:");
String psw = in.next();
User u = new User(name,psw);
list.add(u);//新增到list中
}
System.out.println(list.toString());
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
import java.util.*;
@Override
public String toString() {
return "User [name=" + name + ", passWord=" + passWord + "]";
}
public class TestUser {
public static void main(String[] args) {
List<User> list = new ArrayList<User>();
Scanner in = new Scanner(System.in);
for(int i=0;i<3;i++){
System.out.print("輸入物件的名字:");
String name =in.next();
System.out.print("輸入物件的密碼:");
String psw = in.next();
User u = new User(name,psw);
list.add(u);//新增到list中
}
System.out.println(list.toString());
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}