我试图让这个程序从数组列表中获取密码。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