iHeff
iHeff

Reputation: 249

Reverse a row in Java

I have to reverse a row from an array.

The user inputs the the rows and columns and then inputs numbers to fill the array (I'll give an example below)

I need to then take that inputted data and reverse it and fill the array again.

Example:

3 3 (user input rows and cols)

1 2 3 (user input filling the array with appropriate number of elements)

3 2 1

2 1 3

Needs to be:

2 1 3

3 2 1

1 2 3

The code I have right now was just lots of aimless guessing (and some similar questions on google and here) and right now I can only get it to swap to

1 2 3

3 2 1

1 2 3

The code I have to achieve this:

   public static void reverseRows(int[][] inTwoDArray)
{

   for (int row = inTwoDArray.length-1; row >= 0; row--)
   {
      for (int col = inTwoDArray[0].length-1; col >= 0; col--)
      {
         inTwoDArray[row][col] = inTwoDArray[inTwoDArray.length-1-row][col];
      }
   }

   printArray(inTwoDArray);
}

Any help would be very appreciated, thanks!

Upvotes: 0

Views: 509

Answers (3)

Brandon
Brandon

Reputation: 23498

public static int[][] fliprows(int array[][]) {

    int temp[][] = new int[array.length][array[0].length];
    int j;
    for (int i = 0; i < array.length; ++i) {
        j = array.length - 1 - i;       
        temp[i] = array[j];
    }
    return temp;
}

public static int[][] flipcolumns(int array[][]) {
    int temp[][] = new int[array.length][array[0].length];

    for (int i = 0; i < array.length; ++i) {
        for (int j = 0; j < array[i].length; ++j) {
            temp[i][j] = array[i][array[i].length - j - 1];
        }
    }
    return temp;
}

public static void main(String[] args) {
    int array[][] = {{1, 2, 3}, {3, 2, 1}, {2, 1, 3}};
    array = fliprows(array);

    for (int i = 0; i < array.length; ++i) {
        for (int j = 0; j < array[i].length; ++j)
            System.out.print(array[i][j]);

        System.out.println();
    }
}

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

public class ReverseArray {
    public static void main(String[] args) {
        int[][] arr = {{1,2,3},{3,2,1},{2,1,3}};
        reverseRows(arr);
    }

    public static void reverseRows(int[][] inTwoDArray) {
        List<int[]> tmp = Arrays.asList(inTwoDArray);
        Collections.reverse(tmp);
        inTwoDArray = tmp.toArray(inTwoDArray);

        for(int[] outer:inTwoDArray){
            for(int inner:outer){
                System.out.print(inner + " ");
            }
            System.out.println();
        }
    }
}

Upvotes: 1

BevynQ
BevynQ

Reputation: 8259

You need to store the value you are swapping in a temporary variable, also swapping a row element by element is not necessary.

public static void reverseRows(int[][] inTwoDArray){
    int lastIndex = inTwoDArray.length-1;
    for (int row = 0; row < inTwoDArray.length/2; row++){
        int[] temp = inTwoDArray[row];
        inTwoDArray[row] = inTwoDArray[lastIndex - row];
        inTwoDArray[lastIndex - row] = temp;
    }
    printArray(inTwoDArray);
}

Upvotes: 3

Related Questions