Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with 2D arrays!  RSS feed

 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright so I have as an input, a 2D array of type Piece, and I have to basically create another 2D array with essentially the same values as what was in the original array and that will be the output (of type Piece as well). I am not allowed to just do this:

NewBoard = oldBoard;

I have a strong idea that I should make a loop, but I am not exactly sure what to do.

I am new to programming! Here is my work so far.


public class Game {

final int ROWS = 6;
final int COLS = 8

Piece [][] theBoard = new Piece[][];


public Board(Piece[][] oldBoard) {


}

}

Thanks!!!
 
Mohamed Sanaulla
Bartender
Posts: 3174
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Array Tutorial will be of some help.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really want to create another new array? Or do you want a reference to your existing array? If a reference to your existing array is okay, then you should be able to assign the reference...

...assuming these variables are properly declared. So why exactly do you say you are "not allowed" to use this? Is the compiler stopping you for some reason? Or is this a requirement?

If it's a requirement, and you really want a separate copy of theBoard, then you need to distinguish between a "shallow copy" and a "deep copy." That means, whether the new array will reference the same Piece objects as the original, or whether each Piece instance also needs to be copied.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!