我試圖讓這個程序從數組列表中獲取密碼。Java陣列列表獲得
import java.util.ArrayList;
public class CompanyDatabase {
public ArrayList<Person> getPeople() {
ArrayList<Person> people = new ArrayList<Person>();
String[] u = {"Joe","Stan","Leo","John","Sara","Lauren"};
String[] p = {"pass4321", "asdfjkl", "genericpw", "13579", "helloworld", "companypass"};
for(int j = 0; j < u.length; j++){
Person temp = new Person(u[j],p[j]);
people.add(temp);
}
return people;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class CompanyDatabaseDriver {
private static Scanner scan = new Scanner(System.in));
public static void main(String args[]) {
CompanyDatabase bcData = new CompanyDatabase();
ArrayList<Person> people = bcData.getPeople();
// what i tried
System.out.println(bcData.getPeople());
// also tried this
System.out.println(people.get(1));
}
}
輸出是
[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]
或只是
[email protected]
因爲我試過第二件事。
每次程序運行時,具體的數字/字母組合似乎都會改變。有沒有辦法指定從數組列表中顯示哪個字符串?
您需要在Person類中使用「toString()」方法。 – 2012-03-17 20:23:31