-5

现在我必须编写一个 java boolean isSymmetric () 函数,如果调用矩阵是对称矩阵,则返回 true;否则返回false。谁能帮我这里的代码或从哪里开始?任何答案将不胜感激。

4

1 回答 1

1

You should just Google this stuff. There were plenty of answers out there.

But all you have to do is check if (a,b) is the same as (b,a).

public static boolean isSymetric(int[][] array){
    for(int a = 0; a < array.length; a++){ 
        for(int b = 0; b < array.length; b++){
            if(array[a][b]!=array[b][a]){
                return false;
            }
        }
    }
    return true;
}

In this method the outer most for loop goes through the rows and the inner for loop is going through the columns.

You just have to go through each and every element in the matrix. If array[a][b] == array[b][a] then you can check the next one. If they are not the same then this matrix is not symmetric.

于 2014-04-21T19:53:56.237 回答