Java中的双数组初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18578864/
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
Double array initialization in Java
提问by Prateek
I was reading a book on Javaand came across an example in which an array of type double was initialized in a way that I haven't seen before. What type of initialization is it and where else can it be used?
我正在阅读一本关于Java的书,并遇到了一个示例,其中以我以前从未见过的方式初始化了 double 类型的数组。它是什么类型的初始化,还可以在哪里使用?
double m[][]={
{0*0,1*0,2*0,3*0},
{0*1,1*1,2*1,3*1},
{0*2,1*2,2*2,3*2},
{0*3,1*3,2*3,3*3}
};
回答by sara
double m[][]
declares an array of arrays, so called multidimensional array.
double m[][]
声明一个数组数组,即所谓的多维数组。
m[0]
points to an array in the size of four, containing 0*0,1*0,2*0,3*0.
Simple math shows the values are actually 0,0,0,0.
m[0]
指向一个大小为四的数组,包含 0*0,1*0,2*0,3*0。简单的数学运算表明这些值实际上是 0,0,0,0。
Second line is also array in the size of four, containing 0,1,2,3.
第二行也是大小为4的数组,包含0、1、2、3。
And so on...
等等...
I guess this mutiple format in you book was to show that 0*0 is row 0 column 0, 0*1 is row 0 column 1, and so on.
我猜你书中的这种多重格式是为了显示 0*0 是第 0 行第 0 列,0*1 是第 0 行第 1 列,依此类推。
回答by nneonneo
This is array initializer syntax, and it can only be used on the right-hand-side when declaring a variable of array type. Example:
这是数组初始值设定项语法,它只能在声明数组类型的变量时在右侧使用。例子:
int[] x = {1,2,3,4};
String y = {"a","b","c"};
If you're not on the RHS of a variable declaration, use an array constructor instead:
如果您不在变量声明的右侧,请改用数组构造函数:
int[] x;
x = new int[]{1,2,3,4};
String y;
y = new String[]{"a","b","c"};
These declarations have the exact same effect: a new array is allocated and constructed with the specified contents.
这些声明具有完全相同的效果:使用指定的内容分配和构造一个新数组。
In your case, it might actually be clearer (less repetitive, but a bit less concise) to specify the table programmatically:
在您的情况下,以编程方式指定表实际上可能更清晰(较少重复,但不那么简洁):
double[][] m = new double[4][4];
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
m[i][j] = i*j;
}
}
回答by Cort Ammon
It is called an array initializer and can be explained in the Javaspecification 10.6.
它被称为数组初始值设定项,可以在Java规范 10.6 中进行解释。
This can be used to initialize any array, but it can only be used for initialization (not assignment to an existing array). One of the unique things about it is that the dimensions of the array can be determined from the initializer. Other methods of creating an array require you to manually insert the number. In many cases, this helps minimize trivial errors which occur when a programmer modifies the initializer and fails to update the dimensions.
这可以用于初始化任何数组,但它只能用于初始化(不能分配给现有数组)。它的独特之处之一是可以从初始化程序确定数组的维度。创建数组的其他方法需要您手动插入数字。在许多情况下,这有助于最大限度地减少程序员修改初始值设定项而未能更新维度时发生的微不足道的错误。
Basically, the initializer allocates a correctly sized array, then goes from left to right evaluating each element in the list. The specification also states that if the element type is an array (such as it is for your case... we have an array of double[]), that each element may, itself be an initializer list, which is why you see one outer set of braces, and each line has inner braces.
基本上,初始化程序分配一个正确大小的数组,然后从左到右评估列表中的每个元素。该规范还指出,如果元素类型是一个数组(例如对于您的情况...我们有一个 double[] 数组),则每个元素本身可能是一个初始化列表,这就是您看到一个的原因外部大括号,每行都有内部大括号。
回答by C.Lechner
If you can accept Double Objects than this post is helpful: Initialization of an ArrayList in one line
如果您可以接受 Double Objects,那么这篇文章会很有帮助: Initialization of an ArrayList in one line
List<Double> y = Arrays.asList(null, 1.0, 2.0);
Double x = y.get(1);
回答by SGal
You can initialize an array by writing actual values it holds in curly braces on the right hand side like:
您可以通过在右侧的花括号中写入实际值来初始化数组,例如:
String[] strArr = { "one", "two", "three"};
int[] numArr = { 1, 2, 3};
In the same manner two-dimensional array or array-of-arrays holds an array as a value, so:
以同样的方式,二维数组或数组数组将数组作为值保存,因此:
String strArrayOfArrays = { {"a", "b", "c"}, {"one", "two", "three"} };
Your example shows exactly that
你的例子正好说明了这一点
double m[][] = {
{0*0,1*0,2*0,3*0},
{0*1,1*1,2*1,3*1},
{0*2,1*2,2*2,3*2},
{0*3,1*3,2*3,3*3}
};
But also the multiplication of number will also be performed and its the same as:
但也将执行数字的乘法,其与以下相同:
double m[][] = { {0, 0, 0, 0}, {0, 1, 2, 3}, {0, 2, 4, 6}, {0, 3, 6, 9} };