[英]java two-dimensional array out of extension
i have this simple code: 我有这个简单的代码:
double[][] params = new double[][]{{197.0,258.0,427.0,426.0,507.0,524.0,386.0},
{345.0,473.0,484.0,422.0,406.0,291.0,289.0}};
for (int i = 0; i <= params.length; i++) {
for (int j = 0; j <= params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); //line 29
}
}
and the error: 和错误:
> Caused by: java.lang.ArrayIndexOutOfBoundsException: 7 at
> com.eyecom.gen.generate_xml(gen.java:29)
Why do I get this exception? 为什么会出现此异常?
In Java, arrays are zero based. 在Java中,数组是从零开始的。 Here you're exceeding the array bounds.
在这里,您超出了数组范围。 Use
采用
for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
Try 尝试
for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); // line 29
}
}
indexes in array are from 0
to length-1
so you don't want to use index equal to length
. 数组中的索引从
0
到length-1
所以您不想使用等于length
索引。
Change the for loop construct 更改for循环结构
for (int i = 0; i <= params.length; i++) {
for (int j = 0; j <= params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); //line 29
}
}
to 至
for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); //line 29
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.