二维数组中的 Arrayindexoutofbounds java
Arrayindexoutofbounds in an 2d array java
我正在尝试从二维字符串数组中获取默认的空值。
到目前为止我已经知道了。
String[][] sterren = new String[12][37];
int h = 12;
String leeg = "";
while(h > 0){
int b = 37;
sterren[h][b] = leeg;
while(b > 0){
sterren[h][b] = leeg;
b = b-1;
}
h = h-1;
}
h = 高度和 b = 宽度
当我尝试执行此操作时,我在行 sterren[h][b] = leeg;
上得到一个 arrayindexoutofbounds 12。为什么会这样,因为我明明做了12列37行。
new String[12][37]
表示每个数组的length
为12
和37
,分别可以容纳12个和37个Object。因为数组从索引 0
开始,这意味着它们只有索引 0
- length - 1
。这意味着如果数组的长度为 12
,它的索引为 0
-11
。在此示例中,您尝试添加到外部数组中的索引 12
和索引 37
到内部数组(其中它们的最后一个索引是 36
)。
为了使这项工作有 h = 11
和 b = 36
并使 while
循环 while h >=0
和 while b >= 0
.
我正在尝试从二维字符串数组中获取默认的空值。
到目前为止我已经知道了。
String[][] sterren = new String[12][37];
int h = 12;
String leeg = "";
while(h > 0){
int b = 37;
sterren[h][b] = leeg;
while(b > 0){
sterren[h][b] = leeg;
b = b-1;
}
h = h-1;
}
h = 高度和 b = 宽度
当我尝试执行此操作时,我在行 sterren[h][b] = leeg;
上得到一个 arrayindexoutofbounds 12。为什么会这样,因为我明明做了12列37行。
new String[12][37]
表示每个数组的length
为12
和37
,分别可以容纳12个和37个Object。因为数组从索引 0
开始,这意味着它们只有索引 0
- length - 1
。这意味着如果数组的长度为 12
,它的索引为 0
-11
。在此示例中,您尝试添加到外部数组中的索引 12
和索引 37
到内部数组(其中它们的最后一个索引是 36
)。
为了使这项工作有 h = 11
和 b = 36
并使 while
循环 while h >=0
和 while b >= 0
.