Java-实例化未知数量的实体

Java-实例化未知数量的实体,java,Java,我有一个名为Person的实体,其中有两个参数:姓名和年龄。 我不知道我可能需要多少实体,这就是我如何使用预定义数量的实体: private Person[] person = new Person[3]; //With this I'm saying that there will be only 3... 现在,我不知道我会有多少人,所以,我怎么能用一个列表来做到这一点 List<Person> personList = new List<Person>(); /

我有一个名为Person的实体,其中有两个参数:姓名和年龄。
我不知道我可能需要多少实体,这就是我如何使用预定义数量的实体:

private Person[] person = new Person[3]; //With this I'm saying that there will be only 3... 
现在,我不知道我会有多少人,所以,我怎么能用一个列表来做到这一点

List<Person> personList = new List<Person>(); //random guessing
List personList=new List()//随机猜测

您需要使用
列表的具体实现,如下所示:

List<Person> personList = new ArrayList<Person>();
List personList=new ArrayList();

您需要使用
列表的具体实现,如下所示:

List<Person> personList = new ArrayList<Person>();
List personList=new ArrayList();

是的,列表就是您要查找的内容。ArrayList是您想要实例化的实际类

List<Person> personList = new ArrayList<Person>();
personList.add(new Person());

是的,清单就是你们要找的。ArrayList是您想要实例化的实际类

List<Person> personList = new ArrayList<Person>();
personList.add(new Person());

你的意思是说你有一个名为Person的对象吗?看看列表的实现。然后根据您的要求去适当的具体实施清单。首先,使用ArrayList。List personList=新建ArrayList();你的意思是说你有一个名为Person的对象吗?看看列表的实现。然后根据您的要求去适当的具体实施清单。首先,使用ArrayList。List personList=新建ArrayList();