在java中比较2D数组中的值
我正在研究一个问题,需要在java中比较2D数组中的值。例如:在java中比较2D数组中的值,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我正在研究一个问题,需要在java中比较2D数组中的值。例如: int N = 2, c = 2; int [][] arr = new int[N][c]; System.out.println("Enter the values to a 2D array: "); for(int i=0; i<N;i++) { for (int j=0;j<c;j++) { arr[i][j]=in.nextInt(); } } 请建议我一种执行
int N = 2, c = 2;
int [][] arr = new int[N][c];
System.out.println("Enter the values to a 2D array: ");
for(int i=0; i<N;i++) {
for (int j=0;j<c;j++) {
arr[i][j]=in.nextInt();
}
}
请建议我一种执行此操作的方法-单独比较值。谢谢。
arr1[i]
是一个整数数组,不是整数,因此无法将其与整数进行比较arr1[i][j]
是一个int
,可以与整数进行比较
if(arr[i][j]>=0)
是有效语法,但不清楚这是否是您想要的语法。arr1[i]
是整数数组,而不是整数,因此无法将其与整数进行比较arr1[i][j]
是一个int
,可以与整数进行比较
如果(arr[i][j]>=0)
是有效语法,但不清楚这是否是您想要的语法。要比较二维数组的值,您应该检查该数组的每个值
2x2阵列
当i=0时,j=0
x
当i=0时,j=1
。x
当i=1时,j=0
x
当i=1时,j=1
。x
for(int i=0;i要比较二维数组的值,您应该检查该数组的每个值
2x2阵列
当i=0时,j=0
x
当i=0时,j=1
.x
当i=1时,j=0
x
当i=1时,j=1
.x
for(int i=0;i您正在二维数组中存储整数。如果有帮助,您可以通过考虑行和列来可视化地建模二维数组-每一行和列对引用其在数组中各自的存储位置。示例:
for(int i=0; i<N;i++) {
for (int j=0;j<c;j++) {
if (arr[i]>=0 && arr[j]>=0) {
//Some operation//
}
}
}
arr[0][1] = 5; // sets the value of row [0] column [1] to 5
在第二个嵌套的“for”循环(您遇到一些问题的循环)中,您错误地引用了2D数组的值。请记住,您必须指定要引用的对-arr[int][int]
if (arr[i]>=0 && arr[j]>=0); // incorrect way of referencing the desired respective location in the 2D array
使用语法准确的“if”语句修改嵌套的“for”循环:
for(int i=0; i<N; i++)
for (int j=0; j<c; j++) // side note: consider using final constant(s) replacing variables N and c. In your case, you are explicitly referencing two integers that store the same value - 2
if (arr[i][j]>=0)
System.out.println("Array Position [" + i + "][" + j + "] is greater than or equal to 0");
for(int i=0;i您正在二维数组中存储整数。如果有帮助,您可以通过考虑行和列来可视化地建模二维数组-每一行和列对引用其在数组中各自的存储位置。示例:
for(int i=0; i<N;i++) {
for (int j=0;j<c;j++) {
if (arr[i]>=0 && arr[j]>=0) {
//Some operation//
}
}
}
arr[0][1] = 5; // sets the value of row [0] column [1] to 5
在第二个嵌套的“for”循环(您遇到一些问题的循环)中,您错误地引用了2D数组的值。请记住,您必须指定要引用的对-arr[int][int]
if (arr[i]>=0 && arr[j]>=0); // incorrect way of referencing the desired respective location in the 2D array
使用语法准确的“if”语句修改嵌套的“for”循环:
for(int i=0; i<N; i++)
for (int j=0; j<c; j++) // side note: consider using final constant(s) replacing variables N and c. In your case, you are explicitly referencing two integers that store the same value - 2
if (arr[i][j]>=0)
System.out.println("Array Position [" + i + "][" + j + "] is greater than or equal to 0");
用于(int i=0;i如何单独比较这些值..有什么方法吗?@Dev compare什么?整数数组与0的比较是什么意思?你想将数组中的每个数字与0进行比较吗?谢谢你的帮助。我搞错了。它如何单独比较这些值..有什么方法吗?@Dev compare什么?i数组ntegers到0?这种比较的意义是什么?您想将数组中的每个数字都与0进行比较吗?谢谢您的帮助。我弄错了。不清楚您想要实现什么。不清楚您想要实现什么。arr
不需要两个整数。arr[i]
是一个有效的整数数组,但无法与0进行比较。感谢您的帮助。了解其背后的逻辑。arr
不需要两个整数。arr[i]
是一个有效的整数数组,但无法与0进行比较。感谢您的帮助。了解其背后的逻辑。