java创建list对象_java – 创建一个包含多个对象类型的arraylist?

用户定义的类阵列列表示例

import java.util.*;

public class UserDefinedClassInArrayList {

public static void main(String[] args) {

//Creating user defined class objects

Student s1=new Student(1,"AAA",13);

Student s2=new Student(2,"BBB",14);

Student s3=new Student(3,"CCC",15);

ArrayList al=new ArrayList();

al.add(s1);

al.add(s2);

al.add(s3);

Iterator itr=al.iterator();

//traverse elements of ArrayList object

while(itr.hasNext()){

Student st=(Student)itr.next();

System.out.println(st.rollno+" "+st.name+" "+st.age);

}

}

}

class Student{

int rollno;

String name;

int age;

Student(int rollno,String name,int age){

this.rollno=rollno;

this.name=name;

this.age=age;

}

}

节目输出:

1 AAA 13

2 BBB 14

3 CCC 15