-1

I need to flip a 1-D 64-element array of shorts (I can switch to ints if it's easier, but I assume the same processes will work for either) on it's head in Java. I represent it here as a square table for ease of understanding, since the actual problem is on a chessboard.

For example:

short[] example = new short[]
{
    1, 2, 3,
    4, 5, 6,
    7, 8, 9
};

would become:

7 8 9
4 5 6
1 2 3

Please note that this is NOT the same as reversing the array (every answerer to similar questions I have found has made this mistake, hence my having to ask!). Reversing the array would give:

9 8 7
6 5 4
3 2 1

Apologies if I've missed any important info, any help is appreciated!

EDIT: The array is 1D and contains 64 elements, so short[64], and the reversed array is separate to the original. As far as what I've tried, I'm just struggling to wrap my head around it. I know how to reverse the array, but that's not what I'm after, and I had originally tried to reverse the index using:

byte index = (byte)(((byte)(position + 56)) - (byte)((byte)(position / 8) * 16));

Which is a code snippet I found on Chessbin, but this returns incorrect values and gives IndexOutOfBounds errors. In hindsight it's not clear to me if that code is meant to flip the index or reverse it. Since maths is not my strong suit, I tried to work around it with separate arrays.

10
  • 12
    You've missed what you have tried Commented May 13, 2013 at 18:07
  • 3
    What does this array look like in code? Is it short[][]? Have you tried anything yet? Was there an error? Commented May 13, 2013 at 18:08
  • 1
    Is this a multidimensional array ? Commented May 13, 2013 at 18:09
  • 3
    Is this a 2D array? Otherwise I don't see how you would know where to start flipping the array... Commented May 13, 2013 at 18:10
  • 1
    Why not use a multidimensional array for this? How do you know how many "rows" there should be (I know it is probably 8, but is this always the case)? Commented May 13, 2013 at 18:40

2 Answers 2

2

My proposal would be like this:

public class Flipper {

    public short[] flip(short[] array, int columns) {
        short[] flipped = new short[array.length];
        for(int i=0;i<array.length;i++){
            int row = (i/columns); //use the fact that integer/integer is rounded down
            int column = (i%columns);
            flipped[i] = array[array.length-((columns*(row+1))-column)];
        }
        return flipped;
    }

}

Which can be tested with:

public class FlipperTest {

    private Flipper flipper = new Flipper();

    @Test
    public void test() {
        short[] array = new short[]{1,2,3,4,5,6,7,8,9};
        short[] actualResult = flipper.flip(array, 3);
        assertThat(actualResult, equalTo(new short[]{7,8,9,4,5,6,1,2,3}));
    }

}

Hope the code is self-explanatory

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for a solution with single loop. Not in-place, but could easily be made in-place. Less readable for me. Note: "row" is 1-based while "column" is 0-based; that merits some mention in the code, preferably in the variable name, or at least in-line documentation.
@Andy Thomas-Cramer - Agree, answer improved
1

You have a physical 1D array representing a logical 2D array, and you want to swap rows. You can do this partly by mapping 2D array indices into a 1D array index.

Let height be the number of rows, and width be the number of columns.

for ( int i = 0; i < height/2; ++i ) {
    int k = height - 1 - i;
    for ( int j = 0; j < width; ++j ) {
        short temp = array[i * width + j];
        array[i * width + j] = array[k * width + j];
        array[k * width + j] = temp;
    }
}    

I've written this for readability. You or the compiler may optimize some of the repeated computations.

You might be able to optimize further by using a 2D array, which would allow you to swap references to rows in O(height), rather than copying all the rows in O(height * width).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.