在我的程序中,我添加了另一个类类型的ArrayList,但是当我迭代列表时,我得到了一个错误的地址。 例如我的代码是
ArrayList<Student> stdntList = new ArrayList<Student>();
stdntList.add(new Student("Candace","633501"));
stdntList.add(new Student("Curtis ","634572"));
stdntList.add(new Student("Amber","623343"));
System.out.println(stdntList);
Iterator itr = stdntList.iterator();
while(itr.hasNext())
{
System.out.println("Student: " + itr.next());
}
我的输出是
Student Name: Candace Student ID633501
Student Name: Curtis Student ID634572
Student Name: Amber Student ID623343
[Student@659e0bfd, Student@2a139a55, Student@15db9742]
Student: Student@659e0bfd
Student: Student@2a139a55
Student: Student@15db9742
答案 0 :(得分:2)
为了获取地址,您应该打印类似System.out.println("Student: " + itr.next().getAddress());
就像现在一样,它会打印 .toString()方法中的所有内容。您必须重写此方法才能通过仅调用对类的引用来打印地址。