for (int i = 0; i < list.size(); i++) {
对象的类型 obj=( 对象的类型)list.get(i);
System.out.println(obj.get属性());//通过这个对象就能获得了
}
重新写你那个对象的toString()方法就可以了,譬如你那个对象类为Student
class student{
private int id;
private String password;
private String username;
public String toString(){
return id +"," +password + "," + username;
}
}
这样你就可以直接调用
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
list.get(i).get属性值();能获得属性值
Iterator it=list.iterator();
while (it.hasNext()){
对象类型 dd=(对象类型)it.next();
System.out.println(dd.getUserName());
}
直接用foreach不就好了
public static void main(String [] args){
Student stu1 = new Student();
stu1.setId(1);
stu1.setUsername("川子哥");
stu1.setSex("男");
Student stu2 = new Student();
stu2.setId(2);
stu2.setSex("女");
stu2.setUsername("小花");
ArrayListarrayList = new ArrayList ();
arrayList.add(stu1);
arrayList.add(stu2);
for (Student student : arrayList) {
int a = student.getId();
System.out.println(a);
}
}