简体   繁体   中英

2d Array that swaps all the integers or characters in opposing rows/columns

I feel like I'm getting this problem turned around in my head because rows and columns are easy to mix up. Can someone help show me where I'm going wrong.

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++;
    }
}

Rows are horizontal, columns are vertical. From your description, you have something like this:

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

With a 2d array, you will have var myVar[ROW][COLUMN] So, the value of myVar[1][2] will be 'b', as that is ROW 1 and COLUMN 2 .

For this program it sounds like you need two functions. The first one should swap the values of the rows ( switchRows ), ROW 1 with ROW 2 , so you will have:

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

The value of myVar[1][2] will then be 'y'.

The other should function should swap the values of the columns ( switchColumns ), COLUMN 1 with COLUMN 2 , so you will have

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

The value of myVar[1][2] after doing that will be 'a'.

As a hint on how to do this, swapping an array inside out as described (so [1, 2, 3, 4, 5] to [5, 4, 3, 2, 1] ) is the same as reversing the order of the elements inside the array. Try adding the elements to a new array, but starting at the end of the original and working to the beginning.

EDIT: A couple other notes: You are re-declaring your temp array each iteration through the inner for loop, effectively resetting it.

Also, you really only need a single function to do the actual swapping. In pseudo-code:

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;
}

Try this code. In switchRows we use a pivot variable tmp to swap the values of cell in the rows that are opposites. The expression [anArray.length - i -1][j] gives you the index of the cell in the opossite row but in the same column to [i][j] . Also notice that we iterate over the half of the rows (anArray.length/2) and that you don't need and extra array for the swap operation. For switchColumns the procedure is analogue.

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;
        }
    }
}

Try this for rows. you can play with the main for testing:

    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("-----------------");
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM