簡體   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