This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes expanding a multidimensional array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "expanding a multidimensional array?" Watch "expanding a multidimensional array?" New topic
Author

expanding a multidimensional array?

MR Chen
Ranch Hand

Joined: Aug 10, 2007
Posts: 31
OK, so I'm writing a game, and it has a board. 8x8 squares. Like a checkerboard. I'm representing it with a multidimensional array.

Now, let's suppose that, during the game, I want to expand the board to a 10x10 size (i.e. add one additional square around the border of the old board).

Can I do this with arraycopy()?

The few references that I've seen to arraycopy() have only used single-dimension arrays, so I'm wondering how the syntax would work for a multidimensional array.

Thanks,
Mike
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

A "2D array" in Java is actually a 1D array of 1D arrays; i.e., your 8x8 board is one object that's a 1D array of 8 arrays, and 8 more objects that are 1D arrays of squares.

System.arraycopy() can help you individually copy each of these nine objects, but it can't do it all at once. You're going to have to do something like:


It would be possible to write this with less code; I did it this way to make it very obvious what's going on.


[Jess in Action][AskingGoodQuestions]
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
I would use arraycopy() looping through rows, that is, copying row by row.


This code will copy all rows from board (0 - 7) into rows (1 - 8) of newboard. For each row, it copies 8 positions from board starting at 0, to newboard starting at 1.
You would have to initialize the new cells accordingly...

Edit: I screwed up a little bit on the array declarations, nevermind, look at Ernest's code I rarely use jagged arrays!
Edit II: Just to point out that this code copies the original board centered on the new one, yielding new cells all around. Ernest's version adds rows and columns at the end.

[ October 05, 2007: Message edited by: greg buela ]
[ October 05, 2007: Message edited by: greg buela ]

SCJP 1.5
MR Chen
Ranch Hand

Joined: Aug 10, 2007
Posts: 31
Ernest/greg:

Thanks for your quick responses!

It's good to know that I was on the right track (conceptually speaking).

Much Obliged!

Mike
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: expanding a multidimensional array?
 
Similar Threads
MultiDimensional array doesnt work
multidimensional arrays
multidimensional array confusion
multidimensional array problem
multidimensional arrrays