How do you store three different objects in a list array in Java [closed]

狂风中的少年 提交于 2020-06-23 05:32:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!