public class Test {
public static void main(String... args) {
int[][] arrayOfInts = {
{23, 3, 65, 46},
{65, 55, 2, 3},
{55, 22, 35, 47}
};
int i, j, toFind = 2;
boolean foundIt = false;
search:
for(i = 0; i < arrayOfInts.length; i++) {
for(j = 0; j < arrayOfInts[i].length; j++) {
if(arrayOfInts[i][j] == toFind) {
foundIt = true;
break search;
}
}
}
if(foundIt)
System.out.println("Element found at index " + i + ", " + j);
else
System.out.println("Element not found");
}
}
亲爱的,我在编译上面的代码时遇到了麻烦。当我将整数变量 j 初始化为 0 ( j = 0
) 时,我的代码运行良好。
但我的问题是Why should i initialize j = 0 ?
为什么我variable j might not have been initialized
在行中遇到错误
System.out.println("Element found at index " + i + ", "+ J);
如果我的 int 变量i
正在存储一个值,为什么不能j
存储.. ??
PS Noob在这里..!!