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
Variable-sized Array Initialization in Java
提问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 for
loop, but I hoping there was a simple way to do it.
我知道我可以使用for
循环,但我希望有一种简单的方法来做到这一点。
采纳答案by laginimaineb
int[] myNumbers = new int[size];
Arrays.fill(myNumbers, 0);
see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html
见http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html
回答by Trevor Harrison
int[] MyNumbers = new int[number_of_elements];
回答by BalusC
Alternatively you could use ArrayList
so 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 ArrayList
is part of.
这是一个教程,可以了解有关 Collections API 的更多信息,这ArrayList
是其中的一部分。
回答by Tendayi Mawushe
int[] MyNumbers = new int[number_of_elements];
Since this is an array of int
the array elements will get the default value for int's in Java of 0
automatically.
由于这是int
数组元素的数组,因此将自动获得 Java 中 int 的默认值0
。
If this were an array of Integer
objects 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 Object
array 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 int
array 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);
}
}