寻找指数&具有最多1的二维数组中的列

时间:2014-11-10 15:39:11

标签: java arrays multidimensional-array

编写一个程序,我将0和1和1随机插入一个4 x 4阵列矩阵,然后需要返回列和行索引,其中1' s出现次数最多。我的第一个检查行的方法工作正常,但我在第二个检查列方法中遇到问题。

错误在于它无法找到变量" col"的符号。

public class AssignmentEight_MultiArray {
 public static void main(String[] args) {

 System.out.println("Beginning to insert random integers into array..");

 // Declare array matrix
 int[][] matrix = new int[4][4];

 // For loop to insert integers into the array 
 for (int row = 0; row < matrix.length; row++) {
  for (int col = 0; col < matrix[row].length; col++) { 
  int ranNum = (Math.random() < 0.5) ? 0:1;
  matrix[row][col] = ranNum;
  }
 }

// Display contents of the array
System.out.println("The values of the array are printed below");

showArray(matrix);

System.out.println();

System.out.print("The largest row index: " + scanRow(matrix) + "\n");
System.out.print("The largst column index: " + scanColumn(matrix) + "\n");


} // end main

// method to show array contents 
private static void showArray(int[][] array) {
 for (int row = 0; row < array.length; row++) {
   for (int col = 0; col < array[row].length; col++)
     System.out.print(array[row][col] + "\t");
   System.out.println();
 }
}

// row index method 
private static int scanRow(int[][] array) {
 int result = -1;
 int highest = -1;

for (int row = 0; row < array.length; row++) {
 int temp = 0;
 for (int col = 0; col < array[row].length; col++) {
 // assign current location to temp variable
 temp = temp + array[row][col];
 }

if (temp > highest) {
  highest = temp;
  result = row;
}

}
return result;
} // end of row method

// column index method
private static int scanColumn(int[][] array) {
 int result = -1;
 int highest = -1;

for (int row = 0; row < array.length; row++) {
int temp = 0;  
for (int col = 0; col < array[row].length; col++) {
// assing current location to temp variable
temp = temp + array[row][col];
}

if (temp > highest) {
 highest = temp;
 result = col;
}

}
 return result;
} // end column method  


} // end class 

2 个答案:

答案 0 :(得分:2)

我认为你在谈论这段代码造成的错误:

            for (int col = 0; col < array[row].length; col++) {
// assing current location to temp variable
                temp = temp + array[row][col];
            }

            if (temp > highest) {
                highest = temp;
                result = col;
            }

如果您注意到result = col;之前大括号已关闭 col不存在于for循环之外。

答案 1 :(得分:0)

我认为,一个好的方法,你应该像这样使用变量col

        private static int scanColumn(int[][] array) {
         int result = -1;
         int highest = -1;
// declare and initialize the variable(here you've 'created' it, to then call it on if statement)
         int col = 0;

        for (int row = 0; row < array.length; row++) {
            int temp = 0;
        //declare the variable in the for loop
            for (col = 0; col < array[row].length; col++) {
                // assing current location to temp variable
                temp = temp + array[row][col];
            }

            if (temp > highest) {
             highest = temp;
            result = col;
            }

        }
         return result;
        }