java 创建一个具有相同值/对象的 n 个副本的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14276470/
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-10-31 15:43:18  来源:igfitidea点击:

Create an array with n copies of the same value/object?

javaarrayscopy

提问by X?pplI'-I0llwlg'I -

I want to create an array of size nwith the same value at every index in the array. What's the best way to do this in Java?

我想在数组中的n每个索引处创建一个大小相同的数组。在 Java 中执行此操作的最佳方法是什么?

For example, if nis 5 and the value is the boolean false, the array should be:

例如,如果n是 5 并且值为 boolean false,则数组应为:

= [false, false, false, false, false]

回答by Innokenty

List<Integer> copies = Collections.nCopies(copiesCount, value);

javadoc here.

此处为javadoc 。

This is better than the 'Arrays.fill' solution by several reasons:

由于以下几个原因,这比“Arrays.fill”解决方案更好:

  1. it's nice and smooth,
  2. it consumes less memory (see source code) which is significant for a huge copies amount or huge objects to copy,
  3. it creates an immutable list,
  4. it can create a list of copies of an object of a non-primitive type. That should be used with caution though because the element itself will not be duplicated and get() method will return the same value for every index. It's better to provide an immutable object for copying or make sure it's not going to be changed.
  1. 它很好很光滑,
  2. 它消耗更少的内存(参见源代码),这对于大量副本或要复制的巨大对象来说意义重大,
  3. 它创建了一个不可变的列表,
  4. 它可以创建非原始类型对象的副本列表。但是应该谨慎使用,因为元素本身不会被复制,并且 get() 方法将为每个索引返回相同的值。最好为复制提供一个不可变的对象或确保它不会被更改。

And lists are cooler than arrays :) But if you really-really-really want an array – then you can do the following:

并且列表比数组更酷 :) 但是如果你真的真的真的想要一个数组——那么你可以执行以下操作:

Integer[] copies = Collections.nCopies(copiesCount, value)
                              .toArray(new Integer[copiesCount]);

回答by hsz

You can try it with:

你可以试试:

boolean[] array = new boolean[5];
Arrays.fill(array, false);

Second method with manual array fill:

手动数组填充的第二种方法:

boolean[] array = new boolean[] {false, false, false, false, false};

回答by Brian Agnew

Arrays.fill()will fill an existing array with the same value. Variants exist for primitives and Objects.

Arrays.fill()将用相同的值填充现有数组。基元和 存在变体Objects

回答by assylias

For that specific example, nothing, a boolean[]will be initialised to [false, false, ...]by default.

对于那个特定的例子,什么都没有,默认情况下aboolean[]将被初始化[false, false, ...]

If you want to initialise your array with non-default values, you will need to loop or use Arrays.fillwhich does the loop for you.

如果要使用非默认值初始化数组,则需要循环或使用Arrays.fillwhich 为您执行循环。

回答by Martin Pekár

Or you can do it in the low level way. Make an array with n elements and iterate through all the element where the same element is put in.

或者你可以用低级的方式来做。创建一个包含 n 个元素的数组,并遍历所有放入相同元素的元素。

int[] array = new int[n];

for (int i = 0; i < n; i++)
{
    array[i] = 5;
}

回答by Kent

Arrays.fill(...)is what you are looking for.

Arrays.fill(...)就是你要找的。

回答by Nitin Gupta

try this ..

试试这个 ..

 Boolean [] data = new Boolean[20];
  Arrays.fill(data,new Boolean(false));