如何在 Java 中创建一个新列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/858572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:16:46  来源:igfitidea点击:

How to make a new List in Java

javalistcollections

提问by user93796

We create a Setas:

我们创建一个Set

Set myset = new HashSet()

How do we create a Listin Java?

我们如何List在 Java 中创建一个?

采纳答案by Dan Vinton

List myList = new ArrayList();

or with generics (Java 7or later)

或使用泛型(Java 7或更高版本)

List<MyType> myList = new ArrayList<>();

or with generics (Old java versions)

或使用泛型(旧 Java 版本)

List<MyType> myList = new ArrayList<MyType>();

回答by Alex B

One example:

一个例子:

List somelist = new ArrayList();

You can look at the javadoc for Listand find all known implementing classes of the Listinterface that are included with the java api.

您可以查看Listjavadoc并找到List包含在 java api中的接口的所有已知实现类。

回答by Paulo Guedes

List list = new ArrayList();

Or with generics

或者使用泛型

List<String> list = new ArrayList<String>();

You can, of course, replace string with any type of variable, such as Integer, also.

当然,您也可以将字符串替换为任何类型的变量,例如整数。

回答by Adam Jaskiewicz

First read this, then read thisand this. 9 times out of 10 you'll use one of those two implementations.

先读这个,然后读这个这个。10 次中有 9 次您将使用这两种实现中的一种。

In fact, just read Sun's Guide to the Collections framework.

事实上,只需阅读Sun 的 Collections 框架指南

回答by Aaron Maenpaa

Additionally, if you want to create a list that has things in it (though it will be fixed size):

此外,如果您想创建一个包含内容的列表(尽管它的大小是固定的):

List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");

回答by Blerta

//simple example creating a list form a string array

String[] myStrings = new String[] {"Elem1","Elem2","Elem3","Elem4","Elem5"};

List mylist = Arrays.asList(myStrings );

//getting an iterator object to browse list items

Iterator itr= mylist.iterator();

System.out.println("Displaying List Elements,");

while(itr.hasNext())

  System.out.println(itr.next());

回答by Carl Manaster

Sometimes - but only very rarely - instead of a new ArrayList, you may want a new LinkedList. Start out with ArrayList and if you have performance problems and evidence that the list is the problem, and a lot of adding and deleting to that list - then - not before - switch to a LinkedList and see if things improve. But in the main, stick with ArrayList and all will be fine.

有时——但很少——而不是一个新的 ArrayList,你可能想要一个新的 LinkedList。从 ArrayList 开始,如果您有性能问题并且有证据表明列表是问题所在,并且对该列表进行了大量添加和删除操作 - 那么 - 不是之前 - 切换到 LinkedList 并查看情况是否有所改善。但总的来说,坚持使用 ArrayList 一切都会好起来的。

回答by Sorin Mocanu

Listis just an interface just as Set.

ListSet一样只是一个接口。

Like HashSet is an implementation of a Set which has certain properties in regards to add / lookup / remove performance, ArrayList is the bare implementation of a List.

就像 HashSet 是一个 Set 的实现,它在添加/查找/删除性能方面具有某些属性,ArrayList 是一个 List 的裸实现。

If you have a look at the documentation for the respective interfaces you will find "All Known Implementing Classes" and you can decide which one is more suitable for your needs.

如果您查看相应接口的文档,您会发现“所有已知的实现类”,您可以决定哪一个更适合您的需要。

Chances are that it's ArrayList.

很有可能它是ArrayList

回答by Peter Lawrey

There are many ways to create a Set and a List. HashSet and ArrayList are just two examples. It is also fairly common to use generics with collections these days. I suggest you have a look at what they are

有多种方法可以创建 Set 和 List。HashSet 和 ArrayList 只是两个例子。如今,在集合中使用泛型也相当普遍。我建议你看看它们是什么

This is a good introduction for java's builtin collections. http://java.sun.com/javase/6/docs/technotes/guides/collections/overview.html

这是对java内置集合的很好的介绍。http://java.sun.com/javase/6/docs/technotes/guides/collections/overview.html

回答by Ben Lings

Using Google Collections, you could use the following methods in the Listsclass

使用Google Collections,您可以在Lists类中使用以下方法

import com.google.common.collect.Lists;

// ...

List<String> strings = Lists.newArrayList();

List<Integer> integers = Lists.newLinkedList();

There are overloads for varargs initialization and initialising from an Iterable<T>.

varargs 初始化和从Iterable<T>.

The advantage of these methods is that you don't need to specify the generic parameter explicitly as you would with the constructor - the compiler will infer it from the type of the variable.

这些方法的优点是您不需要像在构造函数中那样显式指定泛型参数 - 编译器将从变量的类型推断它。