繁体   English   中英

二进制运算符的错误操作数类型+第一类型int []和第二类型int

[英]Bad operand type for binary operator + first type int[] and second type int

我正在尝试使用1D数组映射访问2D矩阵定义的值,并希望将特定的索引值存储在变量中。

在使用2D矩阵到1D数组映射的概念上,矩阵包含整数值,但出现“二进制运算符的错误操作数类型+第一类型int []和第二类型int”的错误

导致错误的语句是:

D = fill[ (i-1) * seq_2.length + (j-1)]

我正在尝试访问矩阵填充中的诊断值,即fill [i-1] [j-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)];

    }
}

您说的是fillint类型的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: 15

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM