aspose file tools*
The moose likes Beginning Java and the fly likes Problem moving columns and rows in 2D array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem moving columns and rows in 2D array." Watch "Problem moving columns and rows in 2D array." New topic
Author

Problem moving columns and rows in 2D array.

Jon Mattson
Greenhorn

Joined: Feb 07, 2006
Posts: 19
So I need a method which moves the elements in a column one step up or down and the elements in a row left or right.

I've made this program to test it:



You can compile it and run it. With left and right you change columns and up and down you change rows. With 1 you move the active row 1 step left, with 2 one step right. With 3 you move active column one step up, with 4 one step down.

As you see when moving the elements left or up the first and last element becomes the same. If you move right or down all elements become the same.
I need to do this to make an animation with an image.

When moving up i think the problem could be on this line:



Since its the last element that is wrong. It's like tempArray[0][index1] would have the same value as array[0][index1] which gets changed right in start. I think the problems with moving down or right would be fixed if I knew what was wrong here.

Replies appriciated, been many hours about this.
[ June 15, 2006: Message edited by: Jon Mattson ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Thank you. I did compile it and ran it. If I push -> the active column number gets bigger, up to 4, and similarly for the other keys. There aren't any rows and columns shown on the panel. In fact I don't think you have given us the panel at all. I doesn't work for the 1234 keys, but seems to do what you want with the <- and -> keys.

I don't understand what it is supposed to do, so can't tell what is going wrong.

BTW: Use System.out.println(); instead of System.out.println(""); and your many if statements might be better off as a switch.

CR
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Using the <- and -> keys: you have original.length and original[0].length.
Is this intentional?

I am getting this sort of printout:-
----------------
|02|03|04|05|02|
----------------
|06|07|08|09|10|
----------------
|11|12|13|14|15|
----------------
|16|17|18|19|20|
----------------
|21|22|23|24|25|
----------------
|26|27|28|29|30|
----------------

Active row: 0
Active column: 0
I presume that is what you wanted.
Jon Mattson
Greenhorn

Joined: Feb 07, 2006
Posts: 19
Well i might have been a bit unclear what i wanted.
If you press 1 while 0 is the active row this is what i want to happen:



And this is what happens:



If I press 2 when 0 is the active row, this is what I want to happen:




And this is what happens



I hope this makes it a bit clearer
[ June 15, 2006: Message edited by: Jon Mattson ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
You are going to have to go back to your many for loops and check exactly what you are repeating.

Are you using J2SE5.0? If so you can get rid of the bits about length of String by using the printf method, but it won't work in J2SE1.4.

You would also do well to have a look at the more complicated if statements, like this one:-Try to get them into a simpler format, likeGo through the entire app like that and simplify things. Then try it again. Get rid of the dead wood, then you will be able to see what is going on and get the "for" loops sorted out.
[ June 15, 2006: Message edited by: Campbell Ritchie ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not sure what you're trying to do (even with the simulated output), but do
you really need to change the elements - could you just access the elements in a certain order?

something like this - it just changes to order/display of the columns

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Don't suggest he go down the road of using action commands, or make other major changes to the structure of the app.

I still don't understand quite what the idea of the whole thing is, but it seems to be altering the order of the printouts from the arrays.

There is no need to parse numbers; the KeyEvent.VK_1 sort of event is expressed as an int, so it can be used directly for a switch statement.

There are much easier ways to get a rotating index for an array than the multiple if statements. In fact there is a very simple way to enter an offset value and get 0 1 2 3 4, or 1 2 3 4 0 or 2 3 4 0 1, etc from a for loop.

It is something really basic and simple, first-week stuff, which I haven't told you.

CR
Jon Mattson
Greenhorn

Joined: Feb 07, 2006
Posts: 19
I don't really understand you example ritchie.

But I really wonder why my way doesn't work.

-First I make a copy of the original array:


-Then I pick values from the temp array to the original array. If i want a column to move one step up I do this:

That code is assigning the values of tempArray[i+1]["THE ACTIVE COLUMN"] to array[i]["THE ACTIVE COLUMN"]. That should move all elements in the column (except the one on the top) one step up and i think it works too. But the problem seems to be on the line
which is excecuted when (i+1 < rows) is not true which means it excecutes when the it's the last iteration of the loop. This line is supposed to take the element on the top of the column and place it on the bottom. It takes the value tempArray[0]["THE ACTIVE COLUMN"] (which is the element on the top of the column from the original order) and places it in array[rows-1]["THE ACTIVE COLUMN"] (which is according to my logic the last element in the column in the array with the new order). What i think this line does in reality is that it takes the value from the top of the column in the new order and places it in the last element in the column. But I don't get why it does that.

For me it would be entirely logical for this to work but it doesn't. Could someone explain why?

I hope you understand what I've written since english ain't my native language.

EDIT:

I'll still try to explain for what i need this.
I want to animate an image(a 16x16 tile for my game) of a waterfall. First I load the image to the program. Then I read it's pixeldata and place it in a 1D array. That 1D array I then convert to a 3D array which contains the alpha, red, green, blue values for each pixel. When the image is in this form I want to move the pixels from ONE column one pixel down. Then I change it back to 1D array and to a picture. I repeat this with other columns and it looks like water is falling. I want to have a method which can move the rows or columns in any direction (up, down, left, right) so I can animate other things too. If there's a faster way to move the pixels please let me know.
[ June 18, 2006: Message edited by: Jon Mattson ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
The reason yours isn't working is that it is too complicated.

And as for what you do to get a counter, you ought to have been taught that in your first few weeks of Java programming. You set up an offset, whenever you push the -> key you increase the offset by 1, and whenever you push the <- key you reduce the offset by 1. But make sure your offset never goes less than 0.

To go through the array indices to get 0 1 2 3 4 is easy. To get 2 3 4 0 1 is easy too, providing you remember which of the five basic arithmetic operators you use.

I will give you a hint: +-* and / won't work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
I may have been a bit rude. Sorry.

I'll give you another hint. Compile and run an app like this:-
That will give you a square looking like this:-
00 01 02 03 04 Offset: 0
11 12 13 14 10 Offset: 1
22 23 24 20 21 Offset: 2
33 34 30 31 32 Offset: 3
44 40 41 42 43 Offset: 4

Try altering the app for different sizes of square. Try altering it to move the rows down. Then alter it until you get the sort of offsets you want for your game array.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
. . . and a reminder. If you use array indices, make sure your offset can nevr become < 0.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem moving columns and rows in 2D array.