0

here i want to make duplicate array "arr" for my tile array and make some changes to it, the problem is when i make the changes (in method neighbors) it applies to the original array "tiles" too

public class Board {
    private final int [][] tiles;
    // construct a board from an n-by-n array of blocks
    // (where blocks[i][j] = block in row i, column j)
    public Board(int[][] blocks) {
        tiles = blocks;
    }// all neighboring boards
    public Iterable<Board> neighbors() {
        Queue<Board> q = new LinkedList<>();
        int [][] arr = tiles;
        // do stuff
        if (y+1 < n) {
            int t = arr[x][y];
            arr[x][y] = arr[x][y+1];
            arr[x][y+1] = t;
            Board br = new Board(arr);
            if(!this.equals(br)) {
                q.add(new Board(arr));
            }
        }
        return q;
    } 
}

thanks for your time

Ahmad Fahmy
  • 327
  • 1
  • 2
  • 11
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Ravi Apr 02 '17 at 12:24
  • 1
    this is not duplicate of pass-by-ref or by-value since the array is not passed as method argument – Sharon Ben Asher Apr 02 '17 at 12:26
  • when you make this assignment `int [][] arr = tiles;` you do not create a copy of the array, you just point to the same one with a different pointer. you need to actually create a new array (with the `new ` keyword) and then iterate over the tiles and assign each item to the new array. you can also use `System.arraycopy` see here https://www.tutorialspoint.com/java/lang/system_arraycopy.htm – Sharon Ben Asher Apr 02 '17 at 12:29
  • You haven't copied the array, you've only copied a *reference* to the array. Both variables are referring to the same array. See this question to make a deep copy of the array: http://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java – David Conrad Apr 02 '17 at 12:29
  • yup @DavidConrad thanks you got it write..it was driving me crazy i'm kinda new so really thanks if ur comment was answer i would've approved it :) – Ahmad Fahmy Apr 02 '17 at 12:40
  • also @SharonBenAsher thanks urs is the same – Ahmad Fahmy Apr 02 '17 at 12:41
  • Very well, seems a little light since it's just referring to another question but I've made it an answer. – David Conrad Apr 02 '17 at 12:45

3 Answers3

1

In Java arrays are objects. You can use the clone() method to copy your array. It will perform a shallow copy - it will work fine with your int array.

int [][] arr = new int[tiles.length][];
for(int i = 0; i < tiles.length; i++)
    arr[i] = tiles[i].clone();
  1. copy a 2d array in java
Community
  • 1
  • 1
csirmazbendeguz
  • 598
  • 4
  • 13
1

Arrays are objects in Java. Oracle doc

And objects are not duplicated in that manner. Your variables "tiles" and "arr" are just pointing at the same array now, you haven't copied it.

To copy an array try System.arraycopy:

System.arraycopy( tiles, 0, arr, 0, tiles.length );

See more about System copy here: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)

Ollie in PGH
  • 2,559
  • 2
  • 16
  • 19
0

You haven't copied the array, you've only copied a reference to the array. Both variables are referring to the same array. See this question to make a deep copy of the array: How do I do a deep copy of a 2D array in Java?

Community
  • 1
  • 1
David Conrad
  • 15,432
  • 2
  • 42
  • 54