Java 如何声明带有值的 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21696784/
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
How to declare an ArrayList with values?
提问by alvas
ArrayList or List declaration in Javahas questioned and answered how to declare an empty ArrayList
but how do I declare an ArrayList with values?
Java 中的 ArrayList 或 List 声明已经质疑并回答了如何声明一个空ArrayList
但如何声明一个带值的 ArrayList ?
I've tried the following but it returns a syntax error:
我尝试了以下但它返回一个语法错误:
import java.io.IOException;
import java.util.ArrayList;
public class test {
public static void main(String[] args) throws IOException {
ArrayList<String> x = new ArrayList<String>();
x = ['xyz', 'abc'];
}
}
采纳答案by Maroun
In Java 9+ you can do:
在 Java 9+ 中,您可以执行以下操作:
var x = List.of("xyz", "abc");
// 'var' works only for local variables
Java 8 using Stream
:
Java 8 使用Stream
:
Stream.of("xyz", "abc").collect(Collectors.toList());
And of course, you can create a new object using the constructor that accepts a Collection
:
当然,您可以使用接受 a 的构造函数创建一个新对象Collection
:
List<String> x = new ArrayList<>(Arrays.asList("xyz", "abc"));
Tip: The docscontains very useful information that usually contains the answer you're looking for. For example, here are the constructors of the ArrayList
class:
提示:文档包含非常有用的信息,通常包含您正在寻找的答案。例如,这里是ArrayList
类的构造函数:
Constructs an empty list with an initial capacity of ten.
ArrayList(Collection<? extends E> c)
(*)Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
构造一个初始容量为 10 的空列表。
ArrayList(Collection<? extends E> c)
(*)按照集合的迭代器返回的顺序构造一个包含指定集合元素的列表。
ArrayList(int initialCapacity)
构造一个具有指定初始容量的空列表。
回答by Vimal Bera
You can do like this :
你可以这样做:
List<String> temp = new ArrayList<String>(Arrays.asList("1", "12"));
回答by Drogba
Try this!
尝试这个!
List<String> x = new ArrayList<String>(Arrays.asList("xyz", "abc"));
It's a good practice to declare the ArrayList
with interface List
if you don't have to invoke the specific methods.
如果您不必调用特定方法,那么声明ArrayList
with 接口是一个很好的做法List
。
回答by Puce
Use:
用:
List<String> x = new ArrayList<>(Arrays.asList("xyz", "abc"));
If you don't want to add new elements to the list later, you can also use (Arrays.asListreturns a fixed-size list):
如果以后不想向列表中添加新元素,也可以使用(Arrays.asList返回固定大小的列表):
List<String> x = Arrays.asList("xyz", "abc");
Note: you can also use a static import if you like, then it looks like this:
注意:如果你愿意,你也可以使用静态导入,它看起来像这样:
import static java.util.Arrays.asList;
...
...
List<String> x = new ArrayList<>(asList("xyz", "abc"));
or
或者
List<String> x = asList("xyz", "abc");
回答by Rajendra arora
Use this one:
使用这个:
ArrayList<String> x = new ArrayList(Arrays.asList("abc", "mno"));
回答by Lii
The Guava librarycontains convenience methods for creating lists and other collections which makes this much prettier than using the standard library classes.
该番石榴库包含用于创建列表和其他收藏品,这使得这个更漂亮比使用标准库类的简便方法。
Example:
例子:
ArrayList<String> list = newArrayList("a", "b", "c");
(This assumes import static com.google.common.collect.Lists.newArrayList;
)
(这假设import static com.google.common.collect.Lists.newArrayList;
)