如何在 Java 中填充数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/576855/
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 do I fill arrays in Java?
提问by William
I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
我知道如何正常进行,但我可以发誓你可以像 a[0] = {0,0,0,0}; 一样填写;你是怎么做到的?我确实尝试过谷歌,但我没有得到任何帮助。
采纳答案by Chad Birch
You can also do it as part of the declaration:
您也可以将其作为声明的一部分:
int[] a = new int[] {0, 0, 0, 0};
回答by Zach Scrivena
Arrays.fill()
. The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.
Arrays.fill()
. 该方法针对不同的数据类型进行了重载,甚至还有一种仅填充指定索引范围的变体。
回答by cdmckay
回答by coobird
An array can be initialized by using the new Object {}
syntax.
可以使用new Object {}
语法初始化数组。
For example, an array of String
can be declared by either:
例如,String
可以通过以下任一方式声明一个数组:
String[] s = new String[] {"One", "Two", "Three"};
String[] s2 = {"One", "Two", "Three"};
Primitives can also be similarly initialized either by:
基元也可以通过以下方式类似地初始化:
int[] i = new int[] {1, 2, 3};
int[] i2 = {1, 2, 3};
Or an array of some Object
:
或一些数组Object
:
Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
All the details about arrays in Java is written out in Chapter 10: Arraysin The Java Language Specifications, Third Edition.
有关 Java 中数组的所有详细信息都在第 10 章:Java 语言规范第三版中的数组中写出。
回答by staffan
Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.
Java 中的数组元素在创建时被初始化为默认值。对于数字,这意味着它们被初始化为 0,对于引用它们是 null,对于布尔值它们是 false。
To fill the array with something else you can use Arrays.fill()or as part of the declaration
要使用其他内容填充数组,您可以使用 Arrays.fill()或作为声明的一部分
int[] a = new int[] {0, 0, 0, 0};
There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.
Java 中没有像某些脚本语言那样用算术级数填充数组的捷径。
回答by dasblinkenlight
In Java-8 you can use IntStream
to produce a stream of numbers that you want to repeat, and then convert it to array. This approach produces an expression suitable for use in an initializer:
在 Java-8 中,您可以使用IntStream
生成要重复的数字流,然后将其转换为数组。这种方法产生了一个适合在初始化器中使用的表达式:
int[] data = IntStream.generate(() -> value).limit(size).toArray();
Above, size
and value
are expressions that produce the number of items that you want tot repeat and the value being repeated.
上面的size
和value
是生成要重复的项目数和重复值的表达式。
回答by Cherryishappy
Arrays.fill(arrayName,value);
in java
在 Java 中
int arrnum[] ={5,6,9,2,10};
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Arrays.fill(arrnum,0);
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Output
输出
5 6 9 2 10
0 0 0 0 0
回答by Emir Hadzimahmutovic
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};