0

I am curious as to how Java would handle a situation as follows, I will provides some insight into what it is that I am doing.

I am busy writing a Sudoku Solver for a ProjectEuler problem, I am using a 2D Array of Nodes (Custom Object):

Node[][] grid;

The Following is my Node Class:

public class Node {

    public int x;
    public int y;

    public int value;

    public int quadrant;

    public ArrayList<Integer> possibleNumbers;

    public SudokuNode() {}

    public SudokuNode(int x, int y, int value, int quadrant) {
        this.x = x;
        this.y = y;
        this.value = value;
        this.quadrant = quadrant;
        this.possibleNumbers = new ArrayList<Integer>();
    }

    public ArrayList<Integer> getArray() {
        return possibleNumbers;
    }
}

Now at some stage during the process of Solving the Puzzle I might find that I need to take a guess of sorts, what I would like to do is backup the current state of the grid into another object called Guess, that structure follows as well:

public class Guess {

    public Node[][] grid;

    public int x;
    public int y;

    public Guess(Node[][] puzzle, int x, int y) {
        this.grid = puzzle;
        this.x = x;
        this.y = y;
    }
}

In this object I wish to make a copy of the Current Grid, aswell as the X and Y coordinates of the Node where I took a guess.

What I am curious about is whether or not Java will make a complete copy of the grid, or merely use a pointer to the original one?

A friend of mine is writing a Solver in Javascript, I am aware they are completely different Languages, he ran into a problem where the grid wasn't being backed up as it was still referencing the Original grid he had created and so couldn't revert to the previous state in the event that the guess turned out to be wrong.

I would just like to understand what happens when I say do something like this:

Node[][] backup = currentGrid;

Does it make an entirely new copy or not?

Sorry if this is a duplicate or silly question to ask. I am teaching myself programming and therefore I might have missed something or just don't understand some core concepts.

Razib
  • 10,965
  • 11
  • 53
  • 80
Richard
  • 457
  • 1
  • 6
  • 21

2 Answers2

1

When you pass in object references to constructors or other methods, always the new reference will point to the same object which the original reference was pointing to.

Cat a = new Cat("Harry"); 
Cat b = a;
b.setName("Not Harry");
System.out.println(a.getName()) 

output: "Not Harry".

You could either serialize your object or write the state variables into a file to backup your state of the object.

DesirePRG
  • 6,122
  • 15
  • 69
  • 114
  • Thank you for the example, I could probably write a new Function that creates a new grid, and loop through it and rewrite all the data, but that will slow down the solver quite alot. I guess whether I do that or write it to a file it will still be relatively slow? – Richard Apr 13 '15 at 20:14
  • To be honest I don't think Project Euler questions require serializng or saving states using a file. I think if you think a little bit more you could come up with a design where you can save state in the memory itself. – DesirePRG Apr 13 '15 at 20:22
1

In Java, any operation of the type

Object a = b

will cause a to reference the same object that b was referencing at that point in time.

If the object is modified (via a or b or any other reference), then the change will be reflected no matter what reference is used to access it.

Mecon
  • 977
  • 1
  • 6
  • 17