翻转的2D阵列未正确填充

时间:2014-02-16 18:40:40

标签: arrays multidimensional-array

我的2D数组将无法正确填充我希望原始数组保持不变,这样做,但接下来要翻转的数组。例如111,222,333应该是333,222,111。如果有人可以提供帮助,将不胜感激。

    import java.util.Scanner;
public class Main
{
   public static void main(String args[])
   {
      int rows = 0, cols = 0;
      System.out.println("Please enter the size or your rows first and then columns:");
      Scanner input = new Scanner (System.in);
      rows = input.nextInt();
      cols = input.nextInt();
      int [][] my2DArray = new int [rows][cols];
      System.out.print("Now fill the array with the numbers you would like: ");
      for(int i = 0; i < my2DArray.length; i++)
       {
          for(int j = 0; j < my2DArray[i].length; j++)
       {
          my2DArray[i][j] = input.nextInt();
       }
        System.out.println("");
       }



      System.out.println("Here is the origional unmodified array.");
      for(int i = 0; i < my2DArray.length; i++)
      {
      for(int j = 0; j < my2DArray[i].length; j++)
      {
          System.out.print(my2DArray[i][j] + " ");
      }
      System.out.println("");
      }

      flipMy2DArray(my2DArray);

      }

 public static int[][] flipMy2DArray(int[][] inMy2DArray)
 {
  int [][] flipMy2DArray = new int [inMy2DArray.length][inMy2DArray.length];

  for(int row = flipMy2DArray.length-1; row > 0; row--)
  {
     for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
     {
         flipMy2DArray[row][cols] = inMy2DArray[row][cols];
     }
     System.out.println("");
  }

  for(int row = 0; row < flipMy2DArray.length; row ++)
  {
      for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
      {
          System.out.print(flipMy2DArray[row][cols]+ " ");
      }
      System.out.println("");
  }



       return flipMy2DArray;
   }
}

0 个答案:

没有答案