Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (574) Games in Android Showcase (154) games submitted by our members Games in WIP (620) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 ArrayList of Arrays & "Array.rotate()"  (Read 3329 times) 0 Members and 1 Guest are viewing this topic.
Aoshi

Senior Newbie

 « Posted 2008-08-27 23:45:38 »

(1)
I would like to access the first element of an ArrayList of Arrays:
ArrayList<int[]> MyArrayListOfArrays = new ArrayList<int[]>();

int value = (MyArrayListOfArrays.get(0))[0];
Would that work?

(2)
I have to rotate an array.

Example:
{1,2,3,4}
{2,3,4,1}
{3,4,1,2}
{4,1,2,3}

Jackal von ÖRF

Junior Devvie

 « Reply #1 - Posted 2008-08-28 23:23:52 »

(1)
Yes.

(2)
This algorithm can rotate 2D-arrays with odd dimensions (1x1, 3x3, 5x5 etc). Rotating counter-clockwise is the same as rotating clockwise three times.

 1  2  3  4  5  6  7  8  9 private static char[][] rotateRight(char[][] x) {        char[][] rotated = new char[x.length][x.length];        for (int i = 0; i < x.length; i++) {            for (int j = 0; j < x[i].length; j++) {                rotated[j][(x.length - 1) - i] = x[i][j];            }        }        return rotated;    }

For rotating a 4x4 array you might need a different algorithm, but you can find the right algorithm yourself. First write a unit test for 2x2 rotation and then write code which passes the test. Then write a test for 4x4 rotation, make it pass, and the algorithm should be complete (if you like, you can write one more test for 6x6 just to make sure that it passes).

The trick is that you figure out (on pen and paper) how the coordinates of each element change. Here are the notes which I made when writing the above code.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27 Coordinates when rotating a 3x3 grid right:before   after0-0      0-20-1      1-20-2      2-21-0      0-11-1      1-11-2      2-12-0      0-02-1      1-02-2      2-0Coordinates when rotating a 5x5 grid right:before   after0-0      0-40-1      1-40-2      2-40-3      3-40-4      4-41-0      0-31-1      1-31-2      2-31-3      3-31-4      4-3...

Do you notice a pattern?

JGO Wizard

Medals: 16
Projects: 19

Mojang Specifications

 « Reply #2 - Posted 2008-08-29 17:37:03 »

(2)
I have to rotate an array.

Example:
{1,2,3,4}
{2,3,4,1}
{3,4,1,2}
{4,1,2,3}

This might work:
 1  2  3  4  5  6  7 public int[] rotateLeft(int[] input){    int[] output = new int[input.length];    System.arrayCopy(input, 1, output , 0, input.length-1);    output[input.length-1] = input[0];    return output;}

Play Minecraft!
Riven
« League of Dukes »

« JGO Overlord »

Medals: 953
Projects: 4
Exp: 16 years

 « Reply #3 - Posted 2008-08-29 18:18:42 »

Yeah, I also though he meant 1D rotation, like bit shifting, not 2D.

I'd prefer a copy-less function though...
 1  2  3  4  5  6  7 public void rotateLeft(int[] arr){   int tmp = arr[0];   for(int i=1; i

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Jackal von ÖRF

Junior Devvie

 « Reply #4 - Posted 2008-08-29 21:16:42 »

Yeah, I also though he meant 1D rotation, like bit shifting, not 2D.

So it seems. I had just some time ago been writing a tetris clone, so 2D rotation was the first thing which came into my mind. I interpreted his example as a 4x4 matrix instead of an 1x4 array with 4 states.

Mr_Light

Senior Devvie

Medals: 1

shiny.

 « Reply #5 - Posted 2008-08-30 01:35:15 »

isn't that unboxing going to bite you?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.

JGO Wizard

Medals: 16
Projects: 19

Mojang Specifications

 « Reply #6 - Posted 2008-08-30 08:50:51 »

Yeah, I also though he meant 1D rotation, like bit shifting, not 2D.

I'd prefer a copy-less function though...

 1  2  3  4  5  6  7 public int[] rotateLeft(int[] input){    int tmp = input[0];    System.arraycopy(input, 1, input, 0, input.length-1);    input[input.length-1] = tmp;    return output;}

The specification is unclear whether or not that will copy the data or not, it just says it's done "as if" the data was copied, but that code is more than twice as fast as yours in the test run I did. (an int[100000] rotated (in place) 10000 times via a call to a static method)

Play Minecraft!

Senior Devvie

Medals: 3
Projects: 1

Java games rock!

 « Reply #7 - Posted 2008-08-30 08:59:23 »

I'd find out whether his arrays are going to be variable in size or not. If they're all coming with a length of 4, why the need for all the hassle? Just index the array directly with indexes 0 through 3. Then you won't have to worry about the overhead of looping or possible array copies.
Aoshi

Senior Newbie

 « Reply #8 - Posted 2008-08-30 11:00:43 »

 1  2  3  4  5  6  7 public void rotateLeft(int[] arr){   int tmp = arr[0];   for(int i=1; i

This is exactly what I was looking for. Thank you very much. And sorry for the ambiguous example...
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (33 views) 2015-04-16 10:48:47 Duke0200 (44 views) 2015-04-16 01:59:01 Fairy Tailz (33 views) 2015-04-14 20:13:12 Riven (35 views) 2015-04-12 21:36:37 bus hotdog (50 views) 2015-04-10 02:39:32 CopyableCougar4 (52 views) 2015-04-10 00:51:04 BurntPizza (52 views) 2015-04-06 22:06:58 ags1 (54 views) 2015-04-02 10:58:48 Riven (53 views) 2015-04-01 18:27:05 ags1 (70 views) 2015-03-31 10:55:12
 theagentd 27x BurntPizza 16x wessles 15x kingroka123 11x Rayvolution 11x alwex 11x 65K 11x KevinWorkman 9x kevglass 8x phu004 8x Roquen 7x chrislo27 7x Hanksha 7x Olo 7x ra4king 7x Ecumene 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org