gpt4 book ai didi

java - 如何在 Java 中创建对象列表

转载 作者:行者123 更新时间:2023-12-01 16:52:07 27 4
gpt4 key购买 nike

在 Java 中创建对象列表的等效方法是什么,就像下面的 C# 方法一样(来自网络的示例)?我必须用 Java 写一些东西,但这对我来说是新的。在网络搜索时我似乎也找不到我要找的东西。

List<Author> AuthorList = new List<Author>();

/*以下代码片段创建 Author 对象并将它们添加到列表中。 */

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”:

List<Author> AuthorList = new ArrayList<Author>();

但是原始版本并不键入接口(interface),因此确切的 Java 等效项也许也不应该键入接口(interface)。

关于java - 如何在 Java 中创建对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38127571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com