Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ArrayList of Arrays & "Array.rotate()"  (Read 2154 times)
0 Members and 1 Guest are viewing this topic.
Offline Aoshi

Senior Newbie





« Posted 2008-08-28 01: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}

Any suggestions, please?
Offline Jackal von ÖRF

Junior Member





« Reply #1 - Posted 2008-08-29 01: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   after
0-0      0-2
0-1      1-2
0-2      2-2
1-0      0-1
1-1      1-1
1-2      2-1
2-0      0-0
2-1      1-0
2-2      2-0

Coordinates when rotating a 5x5 grid right:

before   after
0-0      0-4
0-1      1-4
0-2      2-4
0-3      3-4
0-4      4-4
1-0      0-3
1-1      1-3
1-2      2-3
1-3      3-3
1-4      4-3
...


Do you notice a pattern?

Offline Markus_Persson

JGO Wizard


Medals: 8
Projects: 19


Mojang Specifications


« Reply #2 - Posted 2008-08-29 19: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}

Any suggestions, please?

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!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #3 - Posted 2008-08-29 20: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<arr.length; i++)
      arr[i-1] = arr[i];
   arr[arr.length-1] = tmp;
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Jackal von ÖRF

Junior Member





« Reply #4 - Posted 2008-08-29 23: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. Lips Sealed

Offline Mr_Light

Senior Member




shiny.


« Reply #5 - Posted 2008-08-30 03: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.
Offline Markus_Persson

JGO Wizard


Medals: 8
Projects: 19


Mojang Specifications


« Reply #6 - Posted 2008-08-30 10: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!
Offline Addictman

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #7 - Posted 2008-08-30 10: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.
Offline Aoshi

Senior Newbie





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

1  
2  
3  
4  
5  
6  
7  
public void rotateLeft(int[] arr)
{
   int tmp = arr[0];
   for(int i=1; i<arr.length; i++)
      arr[i-1] = arr[i];
   arr[arr.length-1] = tmp;
}


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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (98 views)
2013-05-17 21:29:12

alaslipknot (106 views)
2013-05-16 21:24:48

gouessej (136 views)
2013-05-16 00:53:38

gouessej (131 views)
2013-05-16 00:17:58

theagentd (143 views)
2013-05-15 15:01:13

theagentd (129 views)
2013-05-15 15:00:54

StreetDoggy (172 views)
2013-05-14 15:56:26

kutucuk (194 views)
2013-05-12 17:10:36

kutucuk (196 views)
2013-05-12 15:36:09

UnluckyDevil (202 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.257 seconds with 20 queries.