问题
I need to create a list array in java that can hold three different objects (Surname, forename, Result) and was wondering how to do that
回答1:
The best and also Object Oriented approach is to Create a class with surname, forename and result as attributes(instance variables) like below:
class Student{
private String surname;
private String forename;
private String result;
public Details(String surname, String forename, String res){
//initialize properties here
}
}
now, create a List which only accepts Student
Object.
List<Student> list = new ArrayList<Student>();
list.add(new Student("surname", "forname", "pass"));
来源:https://stackoverflow.com/questions/13712648/how-do-you-store-three-different-objects-in-a-list-array-in-java