【问题标题】:2d Array that swaps all the integers or characters in opposing rows/columns交换相对行/列中所有整数或字符的二维数组
【发布时间】:2013-09-14 15:42:42
【问题描述】:

我觉得这个问题在我的脑海中得到了扭转,因为行和列很容易混淆。有人可以帮我看看我哪里出错了。

public static void switchRows( int[][] anArray ){
    int num = 1;

    for(int i = 0; anArray.length > i; i++){
        for(int j = 0; anArray[i].length > j; j++){
            int[][] temp = new int[anArray.length][anArray[i].length];

            temp[i] = anArray[i];
            anArray[i] = anArray[anArray.length - num];
            anArray[anArray.length - num] = temp[i];

        }
        num++;
    }       
}

public static void switchColumns( char[][] anArray ){       
    int col = 1;

    for(int i = 0; anArray.length > i; i++){
        for(int j = 0; anArray[i].length > j; j++){
            char[][] temp = new char[anArray.length][anArray[i].length];

            temp[j] = anArray[j];
            anArray[j] = anArray[anArray[i].length - col];
            anArray[anArray[i].length - col] = temp[j];

        }
        col++;
    }
}

【问题讨论】:

    标签: java arrays multidimensional-array char int


    【解决方案1】:

    行是水平的,列是垂直的。根据你的描述,你有这样的东西:

           COLUMN 1   | Column 2
    ROW 1 |    'a'    |   'b'
    ROW 2 |    'x'    |   'y'
    

    使用二维数组,您将拥有var myVar[ROW][COLUMN] 因此,myVar[1][2] 的值将是 'b',即ROW 1COLUMN 2

    对于这个程序,听起来您需要两个函数。 第一个应该交换行的值(switchRows),ROW 1ROW 2,所以你将拥有:

           COLUMN 1   | Column 2
    ROW 1 |    'x'    |   'y'
    ROW 2 |    'a'    |   'b'
    

    myVar[1][2] 的值将是 'y'。

    另一个应该函数应该交换列的值(switchColumns),COLUMN 1COLUMN 2,所以你会有

           COLUMN 1   | Column 2
    ROW 1 |    'b'    |   'a'
    ROW 2 |    'y'    |   'x'
    

    这样做后myVar[1][2] 的值将是'a'。

    作为有关如何执行此操作的提示,按照所述将数组从里到外交换(因此 [1, 2, 3, 4, 5][5, 4, 3, 2, 1])与反转数组内元素的顺序相同。尝试将元素添加到新数组中,但从原始数组的末尾开始并一直工作到开头。

    编辑: 其他几点注意事项: 您正在通过内部 for 循环在每次迭代中重新声明您的临时数组,从而有效地重置它。

    另外,你真的只需要一个函数来进行实际的交换。在伪代码中:

    public int[] swap(int[] stuff){
       //do the swapping here
       return swappedStuff;
    }
    
    public int[][] innerSwap(int[][] stuff){
       //go through every char[] and set it equal to swap(char[])
       return innerSwappedStuff;
    }
    

    【讨论】:

      【解决方案2】:

      试试这个代码。在switchRows 中,我们使用主变量tmp 来交换相反行中单元格的值。表达式[anArray.length - i -1][j] 为您提供相反行中单元格的索引,但与[i][j] 在同一列中。另请注意,我们迭代了一半的行(anArray.length/2),并且您不需要额外的数组来进行交换操作。对于switchColumns,该过程是模拟的。

      public static void switchRows(int[][] anArray) {
          for (int i = 0; i < anArray.length/2; i++) {
              for (int j = 0; j < anArray[i].length; j++) {
                  int tmp = anArray[i][j];
                  anArray[i][j] = anArray[anArray.length - i -1][j];
                  anArray[anArray.length - i -1][j] = tmp;
              }
          }
      }
      
      public static void switchColumns(char[][] anArray) {
          for (int i = 0; i < anArray.length; i++) {
              for (int j = 0; j < anArray[i].length/2; j++) {
                  char tmp = anArray[i][j];
                  anArray[i][j] = anArray[i][j + anArray[i].length - 1];
                  anArray[i][j + anArray[i].length - 1] = tmp;
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        对行试试这个。您可以使用 main 进行测试:

            public class ArrayRowSwitcher {
        
                public static int[][] switchRows(int[][] anArray) {
                    int rows = anArray.length;
                    int columns = anArray[0].length;
                    int[][] result = new int[rows][columns];
                    for (int i = 0; rows > i; i++) {
                        for (int j = 0; j < anArray[i].length; j++) {
                            result[rows - i - 1][j] = anArray[i][j];
                        }
        
                    }
                    return result;
                }
        
                public static void main(String[] args) {
        
                    int[][] test = new int[][] {{1,1,1},{2,1,1},{3,1,1},{4,1,1},{5,1,1},{6,1,1}};
                    int[][] result = switchRows(test);
                    for (int i =0;i<test.length;i++){
                        System.out.print("printing row " +i + " --> ");
                        for (int j =0;j<test[0].length;j++){
                        System.out.print(result[i][j]);
                        }
                        System.out.println("-----------------");
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-08
          • 1970-01-01
          • 2020-03-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多