我试图访问使用一维数组映射定义的2D矩阵的值,并希望将该特定的索引值存储在变量中。
该矩阵包含整数值,利用二维矩阵到一维数组映射的概念,我得到了“二进制算子的坏操作数类型+第一类int[]和第二类int”的误差。
导致错误的语句是:
D = fill[ (i-1) * seq_2.length + (j-1)]我试图访问矩阵填充中的诊断值,即filli-1,并希望将其存储在变量D中,seq_2.length是矩阵中列的大小。
守则是
for (i = 1; i <= (seq_1.length); i++) {
for (j = 1; j <= (seq_2.length); j++) {
D = fill[ (i-1) * seq_2.length + (j-1)];
}
}发布于 2017-04-29 20:11:22
您是说fill是int类型的2D数组,D是一个原始类型的整数.获得错误Bad Operand Type for Binary Operator + first type int[] and second type int是因为您试图将fill 2D数组的第一个维度分配给基本数据类型int。考虑一下这个例子:
int[][] array = {{1,2},{3,4}}; // 2D array of type int as an example
for(int i=0; i<2; i++){
System.out.println(array[i]); // this basically is getClass().getName() + '@' + Integer.toHexString(hashCode())
for(int j=0; j<2; j++){
System.out.println(array[j]);
System.out.println(array[i][j]);// this prints out the actual value at the index
}
}
}输出:
[I@15db9742
[I@15db9742
1
[I@6d06d69c
2
[I@6d06d69c
[I@15db9742
3
[I@6d06d69c
4此外,如果要计算正方形2D数组的对角线值,可以执行以下操作:
int[][] array = {{1,2,3},{4,5,6}, {7,8,9}};
int diagonalSum = 0;
for(int i=0; i<3; i++, System.out.println()){
for(int j=0; j<3; j++){
System.out.print(array[i][j]+"\t");
if(j==i){
diagonalSum+=array[i][j];
}
}
}
System.out.println("\nDiagonal Value is: " + diagonalSum);输出:
1 2 3
4 5 6
7 8 9
Diagonal Value is: 15https://stackoverflow.com/questions/43699877
复制相似问题