我正在尝试让这个程序从数组列表中获取密码。
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));
}
}
输出是
[Person@1c9b9ca, Person@c4aad3, Person@1ab28fe, Person@105738, Person@ce5b1c, Person@1bfc93a]
或者只是
Person@1995d80
我尝试的第二件事。
每次运行程序时,具体的数字/字母组合似乎都会发生变化。有没有办法指定从数组列表中显示哪个字符串?
最佳答案
重写 Person
类中的 toString()
关于Java 数组列表获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9753388/