【问题标题】:Comparing elements in row and columns, in this case 1 and 0比较行和列中的元素,在本例中为 1 和 0
【发布时间】:2016-09-22 07:57:15
【问题描述】:

我创建了一个长度相同的二维数组,例如随机填充 1 和 0。

0100
0010
1110
1111

如何对程序进行编码以找到所有 1 和 0 的对角线

这是我目前的代码:

public class Test2dArray {
    public static void main(String[] args) {        
        int row,column;
        System.out.print("Enter the lenghth of matrix:");
        Scanner input = new Scanner(System.in);
        Random rand = new Random ();
        int mSize = input.nextInt();
        int [][] mArray = new int [mSize][mSize];

        for (row=0; row < mSize; row++){
            for(column=0; column < mSize; column++){                
                mArray[row][column]=rand.nextInt(2);
                System.out.print(mArray[row][column]+ " ");
            }
            System.out.println();
        }
    }
}

【问题讨论】:

  • Java !== JavaScript

标签: java arrays multidimensional-array


【解决方案1】:

代码并不完美(我确信它可以优化)但它可以工作

public static void main (String[] args) throws java.lang.Exception {
    int row = 5;
    int column = 5;

    int [][] mArray = fillArray(row, column);
    System.out.println("Rows: " + findRows(mArray));
    System.out.println("Columns: " + findColumns(mArray));
    System.out.println("Diags: " + findDiags(mArray));
}

private static ArrayList<Integer> findRows(int [][] mArray) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int i = 0; i < mArray.length; i++){
        boolean isRow = true;
        for(int j = 0; j < mArray[0].length; j++){                
            if (j > 0 && mArray[i][j] != mArray[i][j - 1]) {
                isRow = false;
                break;
            }
        }
        if (isRow) result.add(i);
    }
    return result;
}

private static ArrayList<Integer> findColumns(int [][] mArray) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int j = 0; j < mArray[0].length; j++){
        boolean isColumn = true;
        for(int i = 0; i < mArray.length; i++){                
            if (i > 0 && mArray[i][j] != mArray[i - 1][j]) {
                isColumn = false;
                break;
            }
        }
        if (isColumn) result.add(j);
    }
    return result;
}

private static ArrayList<Integer> findDiags(int [][] mArray) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int i = 1; i < mArray.length; i++) {
        boolean isDiag = true;
        for (int j = 0; j < i; j++) {
            if (mArray[i - j][j] != mArray[i - j - 1][j + 1]) {
                isDiag = false;
                break;
            }
        }
        if (isDiag) result.add(i);
    }
    for (int i = 0; i < mArray.length - 2; i++) {
        boolean isDiag = true;
        for (int j = i + 1; j < mArray.length - 1; j++) {
            if (mArray[mArray.length - j + i][j] != mArray[mArray.length - j + i - 1][j + 1]) {
                isDiag = false;
                break;
            }
        }
        if (isDiag) result.add(mArray.length + i);
    }

    return result;
}

private static int[][] fillArray(int row, int column) {
    int [][] mArray = new int [row][column];

    Random rand = new Random();
    for (int i = 0; i < row; i++){
        for(int j = 0; j < column; j++){                
            mArray[i][j] = rand.nextInt(2);
            System.out.print(mArray[i][j] + " ");
        }
        System.out.println();
    }

    return mArray;
}

【讨论】:

    【解决方案2】:

    对每一行和每一列进行迭代,并将值相加到rowSumcolumSum。 所以:

        if (rowSum == mSize)
            System.out.print("The row "+i+"is full of ones");
        if (rowSum == 0)
            System.out.print("The row "+i+"is full of zeros");
    

    显然,对于列和对角线也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2015-09-30
      相关资源
      最近更新 更多