如何在Java中检查2D数组是否对角占优

如何在Java中检查2D数组是否对角占优,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,嘿,伙计们,我想做一个函数来检查2D数组是否是对角占优的 有什么想法吗 我已经设法找到了对角线,但是如何检查对角线是否占优势 public static int arraySum(int[][] array){ int total = 0; for (int row = 0; row < array.length; row++) { total += array[row][row]; } return total; } 公共静

嘿,伙计们,我想做一个函数来检查2D数组是否是对角占优的

有什么想法吗

我已经设法找到了对角线,但是如何检查对角线是否占优势

public static int arraySum(int[][] array){
    int total = 0;

    for (int row = 0; row < array.length; row++)
    {

        total += array[row][row];
    }

    return total;
}
公共静态int-arraySum(int[][]数组){
int-total=0;
for(int row=0;row
理论上:在第i行中,检查第i个条目是否小于该行其他值的绝对值之和:

public boolean checkDominance(int[][] matrix)
{
    for (int i = 0; i < matrix.length; ++i)
    {
        int diagEl = Math.abs(matrix[i][i]);
        int sum = 0;
        for (int j = 0; j < matrix[0].lenght; ++j)
        {
            if (i == j) { continue; }
            sum += Math.abs(matrix[i][j]);
        }
        if (sum > diagEl) { return (false); }
    }
    return (true);
}
公共布尔校验优势(int[][]矩阵)
{
对于(int i=0;idiagEl){return(false);}
}
返回(真);
}

理论上:在第i行中,检查第i个条目是否小于该行其他值的绝对值之和:

public boolean checkDominance(int[][] matrix)
{
    for (int i = 0; i < matrix.length; ++i)
    {
        int diagEl = Math.abs(matrix[i][i]);
        int sum = 0;
        for (int j = 0; j < matrix[0].lenght; ++j)
        {
            if (i == j) { continue; }
            sum += Math.abs(matrix[i][j]);
        }
        if (sum > diagEl) { return (false); }
    }
    return (true);
}
公共布尔校验优势(int[][]矩阵)
{
对于(int i=0;idiagEl){return(false);}
}
返回(真);
}
根据,对角占优矩阵是这样的矩阵:

对于矩阵的每一行,一行中对角线项的大小大于或等于该行中所有其他(非对角线)项的大小之和

这只是检查弱对角优势,给定2D数组:

public boolean isDiagonallyDominant(int[][] array) {
    int otherTotal = 0;

    // Loop through every row in the array
    for(int row = 0; row < array.length; row++) {
        otherTotal = 0;

        // Loop through every element in the row
        for(int column = 0; column < array[row].length; column++) {

            // If this element is NOT on the diagonal
            if(column != row) {

                // Add it to the running total
                otherTotal += Math.abs(array[row][column]);
            }
        }

        // If this diagonal element is LESS than the sum of the other ones...
        if(Math.abs(array[row][row]) < otherTotal) {

            // then the array isn't diagonally dominant and we can return.
            return false;
        }
    }
    return true;
}
公共布尔值为对角占优(int[][]数组){
int otherTotal=0;
//循环遍历数组中的每一行
for(int row=0;row
根据,对角占优矩阵是这样的矩阵:

对于矩阵的每一行,一行中对角线项的大小大于或等于该行中所有其他(非对角线)项的大小之和

这只是检查弱对角优势,给定2D数组:

public boolean isDiagonallyDominant(int[][] array) {
    int otherTotal = 0;

    // Loop through every row in the array
    for(int row = 0; row < array.length; row++) {
        otherTotal = 0;

        // Loop through every element in the row
        for(int column = 0; column < array[row].length; column++) {

            // If this element is NOT on the diagonal
            if(column != row) {

                // Add it to the running total
                otherTotal += Math.abs(array[row][column]);
            }
        }

        // If this diagonal element is LESS than the sum of the other ones...
        if(Math.abs(array[row][row]) < otherTotal) {

            // then the array isn't diagonally dominant and we can return.
            return false;
        }
    }
    return true;
}
公共布尔值为对角占优(int[][]数组){
int otherTotal=0;
//循环遍历数组中的每一行
for(int row=0;row
将其他值相加,看看对角线值是否大于其他值的总和。我如何将其他值相加,而不计算对角线中已经存在的值???@KostasMatrix查看我的答案,这就完成了。将其他值相加,看看对角线值是否大于其他值的总和。我如何将其他值相加,而不计算对角线中已经存在的值???@KostasMatrix查看我的答案,这就完成了。@Turing85修复。谢谢你发现了@图灵85修正。谢谢你发现了!