二维阵列操作

时间:2015-09-04 12:50:09

标签: java arrays methods

我很难弄清楚为什么我的方法getLowestInRowgetHighestInRow似乎正在跳过第二列。我有一个嵌套的for循环遍历2D数组,但getLowestInRow的输出不正确,例如对于第4行,它告诉我66是最低的,其中54实际上是最低的。

...

  int[][] testData={{3, 5, 7}, {33, 44, 12}, {1, 45, 67}, {66, 54, 99}};


...

 /* method getHighestInRow  
   takes a 2d int array, the 
   number of rows and the number 
   of columns as input and
   prints the highest number
   of all of the elements 
   in each individual row of
   the array as (int) outputs 
 */  

 public static int getHighestInRow (int[][] x, int rowCount, int colCount)
 {
     int highestInRow = x[0][0];

     for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
     {
       for (int colIndex = 0; colIndex < colCount; colIndex++)
       {
       highestInRow = x[rowIndex][0];
       if (x[rowIndex][colIndex]>highestInRow)
             highestInRow=x[rowIndex][colIndex];
       }
     System.out.println(highestInRow + " is the highest number in row " + (rowIndex+1) + ".");
     highestInRow = 0;
     }
     return highestInRow;
 }

/* method getLowestInRow
   takes a 2d int array as input 
   returns the lowest number
   of all of the elements 
   in each individual row of
   the array as (int) outputs 
 */  

//unfinished
public static int getLowestInRow (int[][] x, int rowCount, int colCount)
{
     int lowestInRow = x[0][0];

     for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
     {
       for (int colIndex = 0; colIndex < colCount; colIndex++)
       {
       lowestInRow = x[rowIndex][0];  
       if (x[rowIndex][colIndex]<lowestInRow)
             lowestInRow=x[rowIndex][colIndex];
       }
     System.out.println(lowestInRow + " is the lowest number in row " + (rowIndex+1) + ".");       
     lowestInRow = 0;
     }
    return lowestInRow;

}

...

2 个答案:

答案 0 :(得分:0)

我已经评论了代码中不需要的代码行。希望这会对你有所帮助。

public static void main(String[] args)
{
    int[][] testData={{3, 5, 7}, {33, 44, 12}, {1, 45, 67}, {66, 54, 99}};

    int rowCount = testData.length;
    int colCount = getNumCols(testData);

/*      getTotal(testData, rowCount, colCount);
    getAverage(testData, rowCount, colCount);
    getRowTotal(testData, rowCount, colCount);
    getColumnTotal(testData, rowCount, colCount);*/
    getHighestInRow(testData, rowCount, colCount);
    getLowestInRow(testData, rowCount, colCount);
}

/* method getTotal
   takes a 2d int array as input 
   returns a sum of all elements of 
   the array as (int) output 
*/  

public static int getTotal(int[][] x, int rowCount, int colCount)
{
    int total = 0; 
    for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
    {
         for (int colIndex = 0; colIndex < colCount; colIndex++)
         {
             total += x[rowIndex][colIndex];   
         }
    }   
    System.out.println("The total is "+ total);
    return total;
}

/* method getAverage
   takes a 2d int array as input 
   returns the average of all elements 
   of the array as (int) output 
*/  

public static double getAverage(int[][] x, int rowCount, int colCount)
{
    int total = 0; 
    for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
    {
         for (int colIndex = 0; colIndex < colCount; colIndex++)
         {
             total += x[rowIndex][colIndex];   
         }
    }     
    double average = (total/(colCount*rowCount));
    System.out.println("The average is "+average);
    return average; 
 }

/* method getRowTotal
   takes a 2d int array as input 
   returns the total of all elements 
   in each individual row of
   the array as (int) outputs 
*/ 

public static int getRowTotal(int[][] x, int rowCount, int colCount)
{
    int rowTotal = 0; 
    int rowIndex = 0;
    for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
    {
        for (int colIndex = 0; colIndex < colCount; colIndex++)
        {
            rowTotal += x [rowIndex][colIndex];      
        }
        System.out.println("The total for row " + (rowIndex+1) + " is " + rowTotal);
        rowTotal = 0;
    }   
    return rowTotal;
}


/* method getColumnTotal
   takes a 2d int array as input 
   returns the total of all elements 
   in each individual column of
   the array as (int) outputs 
*/  

//unfinished    
public static int getColumnTotal(int[][] x, int rowCount, int colCount)
{
    int columnTotal = 0; 
    int rowIndex = 0;
    int colIndex = 0;
    for (colIndex = 0; colIndex < colCount; colIndex++)
    {
        for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
        {             
            columnTotal += x [rowIndex][colIndex];          
        }
        System.out.println("The total for column " + (colIndex+1) + " is " + columnTotal);
        columnTotal = 0;
    }   
    return columnTotal; 
 }

 /* method getHighestInRow  
   takes a 2d int array, the 
   number of rows and the number 
   of columns as input and
   prints the highest number
   of all of the elements 
   in each individual row of
   the array as (int) outputs 
 */  

 public static int getHighestInRow (int[][] x, int rowCount, int colCount)
 {
     int highestInRow = x[0][0];

     for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
     {
       for (int colIndex = 0; colIndex < colCount; colIndex++)
       {
//         highestInRow = x[rowIndex][0];
       if (x[rowIndex][colIndex]>highestInRow)
             highestInRow=x[rowIndex][colIndex];
       }
     System.out.println(highestInRow + " is the highest number in row " + (rowIndex+1) + ".");
     highestInRow = 0;
     }
     return highestInRow;
 }

/* method getLowestInRow
   takes a 2d int array as input 
   returns the lowest number
   of all of the elements 
   in each individual row of
   the array as (int) outputs 
 */  

//unfinished
public static int getLowestInRow (int[][] x, int rowCount, int colCount)
{
     int lowestInRow = x[0][0];

     for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
     {
         lowestInRow = x[rowIndex][0];
       for (int colIndex = 0; colIndex < colCount; colIndex++)
       {
//         lowestInRow = x[rowIndex][0];  
       if (x[rowIndex][colIndex]<lowestInRow)
             lowestInRow=x[rowIndex][colIndex];
       }
     System.out.println(lowestInRow + " is the lowest number in row " + (rowIndex+1) + ".");       

     }
    return lowestInRow;

}


public static int getNumCols (int[][] x)
{
    int rowCount = 0;
    int colCount = 0;

    for (rowCount = 0; rowCount < x.length; rowCount++)
    {
        for (colCount = 0; colCount < x[rowCount].length; colCount++)
        {
        }
    }
    return colCount;     
}

答案 1 :(得分:0)

修改最高和最低功能,如下所示: -

 public static int getHighestInRow (int[][] x, int rowCount, int colCount)
     {
         int highestInRow = 0;

         for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
         {
             highestInRow = x[rowIndex][0];
           for (int colIndex = 0; colIndex < colCount; colIndex++)
           {

           if (x[rowIndex][colIndex]>highestInRow)
                 highestInRow=x[rowIndex][colIndex];
           }
         System.out.println(highestInRow + " is the highest number in row " + (rowIndex+1) + ".");
         highestInRow = 0;
         }
         return highestInRow;
     }

    /* method getLowestInRow
       takes a 2d int array as input 
       returns the lowest number
       of all of the elements 
       in each individual row of
       the array as (int) outputs 
     */  

    //unfinished
    public static int getLowestInRow (int[][] x, int rowCount, int colCount)
    {
         int lowestInRow = 0;

         for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
         {
             lowestInRow = x[rowIndex][0];  
           for (int colIndex = 0; colIndex < colCount; colIndex++)
           {

           if (x[rowIndex][colIndex]<lowestInRow)
                 lowestInRow=x[rowIndex][colIndex];
           }
         System.out.println(lowestInRow + " is the lowest number in row " + (rowIndex+1) + ".");       
         lowestInRow = 0;
         }
        return lowestInRow;

    }