用Java创建对象列表的等效方法是什么(例如下面的C#方法)(来自网络的示例)?我必须用Java编写一些东西,但这对我来说是新的。搜索网络时,我似乎也找不到我想要的东西。
List<Author> AuthorList = new List<Author>();
/ *以下代码片段创建Author对象并将其添加到List中。 * /
AuthorList.Add(new Author("Mahesh Chand", 35, "A Prorammer's Guide to ADO.NET", true, new DateTime(2003,7,10)));
AuthorList.Add(new Author("Neel Beniwal", 18, "Graphics Development with C#", false, new DateTime(2010, 2, 22)));
AuthorList.Add(new Author("Praveen Kumar", 28, "Mastering WCF", true, new DateTime(2012, 01, 01)));
AuthorList.Add(new Author("Mahesh Chand", 35, "Graphics Programming with GDI+", true, new DateTime(2008, 01, 20)));
AuthorList.Add(new Author("Raj Kumar", 30, "Building Creative Systems", false, new DateTime(2011, 6, 3)));
最佳答案
在Java中,您将只使用通用的ArrayList。
import java.util.*;
...
ArrayList<Author> AuthorList = new ArrayList<Author>();
AuthorList.add(new Author("Mahesh Chand", 35, "A Prorammer's Guide to ADO.NET", true, java.time.LocalDateTime.of(2003,7,10)));
...
请注意,您可以将变量键入为“列表”:
List<Author> AuthorList = new ArrayList<Author>();
但是原始语言不能键入接口,因此确切的Java等效语言也不应该输入。