如何在Java的列表数组中存储三个不同的对象 [英] How do you store three different objects in a list array in Java
本文介绍了如何在Java的列表数组中存储三个不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Java中创建一个列表数组,该列表数组可以容纳三个不同的对象(姓,名,结果),并且想知道如何做到这一点
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
推荐答案
最好也是面向对象的方法是创建一个具有姓,名和结果作为属性(实例变量)的类,如下所示:
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
}
}
现在,创建一个仅接受Student
对象的列表.
now, create a List which only accepts Student
Object.
List<Student> list = new ArrayList<Student>();
list.add(new Student("surname", "forname", "pass"));
这篇关于如何在Java的列表数组中存储三个不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文