Java 中的可变大小数组初始化

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

Variable-sized Array Initialization in Java

javaarraysinitialization

提问by daveslab

I have an array of integers in Java that is initialized as follows:

我在 Java 中有一个整数数组,初始化如下:

public int MyNumbers[] = {0,0,0,0};

I would like to, however, initialize the array to a variable-length number of zeroes.

但是,我想将数组初始化为可变长度的零。

private int number_of_elements = 4;
public int MyNumbers[] = {0} * number_of_elements; // ????

I am clueless how to do this being new to Java coming from C. Any suggestions?

作为来自 C 的 Java 新手,我不知道如何做到这一点。有什么建议吗?

EDIT

编辑

I know I could use a forloop, but I hoping there was a simple way to do it.

我知道我可以使用for循环,但我希望有一种简单的方法来做到这一点。

采纳答案by laginimaineb

回答by Trevor Harrison

int[] MyNumbers = new int[number_of_elements];

回答by BalusC

Alternatively you could use ArrayListso that you don't need to worry about the size beforehand. It will dynamically expand the internal array whenever needed.

或者,您可以使用,ArrayList这样您就不必事先担心尺寸。它会在需要时动态扩展内部数组。

List<Integer> numbers = new ArrayList<Integer>();
numbers.add(0);
numbers.add(2);
// ...

Here's a tutorialto learn more about the Collections API, which ArrayListis part of.

这是一个教程,可以了解有关 Collections API 的更多信息,这ArrayList是其中的一部分。

回答by Tendayi Mawushe

int[] MyNumbers = new int[number_of_elements];

Since this is an array of intthe array elements will get the default value for int's in Java of 0automatically.

由于这是int数组元素的数组,因此将自动获得 Java 中 int 的默认值0

If this were an array of Integerobjects then you would have to fill array after creating it as the default value for an object reference is null. To set default values in an Objectarray you can do the following:

如果这是一个Integer对象数组,那么您必须在创建它之后填充数组,因为对象引用的默认值是null. 要在Object数组中设置默认值,您可以执行以下操作:

Integer[] MyNumbers = new Integer[number_of_elements];
java.util.Arrays.fill(MyNumbers, new Integer(0));

The same technique could of course be used to initialize the intarray to values other than zero like so:

当然可以使用相同的技术将int数组初始化为非零值,如下所示:

int[] MyNumbers = new int[number_of_elements];
java.util.Arrays.fill(MyNumbers, 1);

回答by Jimmy Tsai

If you are looking for something similiar to Memset in C, I don't think there is one. The Arrays.fill implementation seems to be the same as a for loop if you look at Javadoc.

如果您正在寻找类似于 C 中的 Memset 的东西,我认为没有。如果您查看 Javadoc,Arrays.fill 实现似乎与 for 循环相同。

However, I came across this article that discusses the same question http://www.searchenginecaffe.com/2007/03/how-to-quickly-reset-value-of-java.htmlIt appears that using system.arraycopy is your better solution and this is the function you can use (copied from the link)

但是,我发现这篇文章讨论了同样的问题 http://www.searchenginecaffe.com/2007/03/how-to-quickly-reset-value-of-java.html看来使用 system.arraycopy 是你的更好的解决方案,这是您可以使用的功能(从链接复制)

public static void bytefill(byte[] array, byte value) {
 int len = array.length;
 if (len > 0)
 array[0] = value;
  for (int i = 1; i < len; i += i) {
    System.arraycopy( array, 0, array, i, ((len - i) < i) ? (len - i) : i);
 }
}