问题描述
我有一个2D数组,其中没有。行是1和否。列是> 1.
I have a 2D array where no. of rows is 1 and no. of columns is > 1.
double[][] T = new double[1][24];
System.out.println(T[1].length);
但是当我打印列的长度时,它表示索引超出范围。
But when i print the length of the columns, its says the index is out of bounds.
但是当我打印以下内容时,
but when i print the following,
System.out.println(T[0].length);
我得到24的结果。但不应该T [0]应该等于1 T [1]等于24?
为什么我收到此错误?
我想,java认为上面的数组是1D数组,因为它只有一行。但是我需要它作为进一步处理的2D数组。有人可以帮忙吗?
I get the result as 24. But shouldn't T[0] should be equal to 1 and T[1] be equal to 24?Why am I getting this error?I suppose, java considers the above array as 1D array since it has only one row. but I need it to be a 2D array for further processes. Could anyone please help?
推荐答案
数组索引从0开始。
如果你的数组长度是 1
(这里是第一个维度),那么你只能引用元素 0
。
If your array length is 1
(for the 1st dimension here), then you can only reference element 0
.
换句话说:
- 声明表示所需的尺寸(此处
1
) - 元素引用表示所需的 0-based index (
0
此处)
- The declaration states the desired size (
1
here) - The element reference states the desired 0-based index (
0
here)
这篇关于数组索引超出绑定的2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!