• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Need help with 2D arrays!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Array Tutorial will be of some help.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I was born with webbed fish toes. This tiny ad is my only friend:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic