Java基础知识强化之集合框架笔记24:ArrayList存储自定义对象并遍历

时间:2023-01-05 05:45:23

1. ArrayList存储自定义对象并遍历

2. 代码示例:

Student.java,如下:

 package cn.itcast_01;

 public class Student {
private String name;
private int age; public Student() {
super();
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

测试类ArrayListDemo2:

 package cn.itcast_01;

 import java.util.ArrayList;
import java.util.Iterator; /*
* ArrayList存储自定义对象并遍历
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList(); // 创建学生对象
Student s1 = new Student("武松", 30);
Student s2 = new Student("鲁智深", 40);
Student s3 = new Student("林冲", 36);
Student s4 = new Student("杨志", 38); // 添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4); // 遍历
Iterator it = array.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
} System.out.println("----------------"); for (int x = 0; x < array.size(); x++) {
// ClassCastException 注意,千万要搞清楚类型
// String s = (String) array.get(x);
// System.out.println(s); Student s = (Student) array.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
}
}

运行效果如下:

Java基础知识强化之集合框架笔记24:ArrayList存储自定义对象并遍历