Java 如何为项目列表设置默认值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9727195/
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 set a default value for items list?
提问by Error_404
I'm trying to convert some python code to java and need to setup a default value of a list. I know the default value, the size of the list and my goal is to setup a default value and then later in my program change them. In python I simply do this(to create 10 items with a value of zero):
我正在尝试将一些 python 代码转换为 java 并且需要设置列表的默认值。我知道默认值、列表的大小,我的目标是设置一个默认值,然后在我的程序中更改它们。在 python 中,我只是这样做(创建 10 个值为零的项目):
list = [0]*10
I am trying to do:
我正在尝试做:
List<Integer> list1 = Arrays.asList(0*10); // it just multiples 0 by 10.
It doest work, I know I can do something like this:
它不起作用,我知道我可以做这样的事情:
for(int i = 0;i<10;i++)
{
list1.add(0);
}
I was wondering if there was an better way(instead of the for loop)?
我想知道是否有更好的方法(而不是 for 循环)?
采纳答案by Mike Samuel
Arrays.fill
lets you avoid the loop.
Arrays.fill
让你避免循环。
Integer[] integers = new Integer[10];
Arrays.fill(integers, 0);
List<Integer> integerList = Arrays.asList(integers);
回答by cdeszaq
In Java, not really. Java is relatively verbose when it comes to this sort of stuff, so there isn't much you can do that is simple, other than a loop like you have.
在 Java 中,并非如此。Java 在处理这类事情时相对冗长,因此除了像您这样的循环之外,没有什么可以做的很简单。
回答by Jon Skeet
There's nothing built into the standard libraries, as far as I'm aware. But you can easily write such a method once and call it from wherever you want. For example:
据我所知,标准库中没有内置任何内容。但是您可以轻松地编写一次这样的方法并从您想要的任何地方调用它。例如:
public static <T> List<T> newArrayList(T value, int size) {
List<T> list = new ArrayList<T>(size);
for (int i = 0; i < size; i++) {
list.add(value);
}
return list;
}
If you never want to change the size of the list (i.e. add or remove elements), Mike Samuel's answer is probably more efficient. Also note that if you're using a mutable type, you may not get what you want:
如果您永远不想更改列表的大小(即添加或删除元素),Mike Samuel 的回答可能更有效。另请注意,如果您使用的是可变类型,则可能无法获得所需内容:
List<StringBuilder> list = newArrayList(new StringBuilder(), 10);
list.get(0).append("Foo");
System.out.println(list.get(5)); // Prints "Foo"
... as each element in the list will be a reference to the same object.
... 因为列表中的每个元素都将是对同一对象的引用。
回答by Mark Byers
Maybe you just need an array?
也许你只需要一个数组?
int[] array = new int[10];
You need a list if you need to change the size of it dynamically. If you don't need this feature, an array may suit your needs, and it will automatically initialize all the values to 0 for you.
如果您需要动态更改其大小,则需要一个列表。如果您不需要此功能,数组可能适合您的需求,它会自动为您将所有值初始化为 0。
回答by Jakub Zaverka
You can try:
你可以试试:
List<Integer> list1 = Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
There are 10 zeroes. You need to know the number of elements at the compile time, but you have only one line. If you don't know the number of elements at compile time, then you can use the suggested Arrays.fill() approach.
有 10 个零。您需要在编译时知道元素的数量,但您只有一行。如果您在编译时不知道元素的数量,那么您可以使用建议的 Arrays.fill() 方法。
回答by amlane86
I would stick with the for loop.
我会坚持使用 for 循环。
BTW... 0*10 = 0 so just enter in the amount you need instead
顺便说一句... 0 * 10 = 0 所以只需输入您需要的数量
ArrayList<Integer> list = new ArrayList<Integer>(10);
ArrayList<Integer> list = new ArrayList<Integer>(10);
回答by Michel Jung
Collections.nCopies
is your friend if you need a list instead of an array:
Collections.nCopies
如果你需要一个列表而不是一个数组,那么它就是你的朋友:
List<Integer> list = Collections.nCopies(10, 0);
If a mutable list is needed, wrap it:
如果需要可变列表,请将其包装起来:
List<Integer> list = new ArrayList<>(Collections.nCopies(10, 0));