0

I have two identical 2D array's of characters however I want to make it so that when I make changes to one it doesn't effect the other. I am assuming it is because they are sharing the same space in memory and was wondering how to stop this.

private char[][] a;
private char[][] b;

a = new char[8][];
b = new char[8][];

while(file.hasNext()) //reads 8 lines from a file and adds it to text to both arrays
{
    char[] constant =  file.nextLine().toCharArray();
    a[i] = constant;
    b[i] = constant;
    i++;
}

then if i were to run the code

a[0][0] = 't';

then it also changes b[0][0] to 't'

  • 1
    Post your code. Variables dont' share memory in Java. – user207421 Feb 17 '14 at 00:11
  • @EJP _Technically_, they might be stored in the same index of a variable table. (busting balls) – Sotirios Delimanolis Feb 17 '14 at 00:21
  • @SotiriosDelimanolis That would implicitly violate both the Java Language Specification #4.5 and the Java Virtual Machine Specification #2.5 (same wording in each). – user207421 Feb 17 '14 at 00:58
  • @EJP JLS Chapter 4.5 is about parameterized types. Can you check again? Or clarify. – Sotirios Delimanolis Feb 17 '14 at 01:01
  • @EJP Maybe I should specify _local_ variables. See [chapter 2.6 of JVM Spec.](http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf). Also, take a look at [this answer.](http://stackoverflow.com/questions/21437699/outofmemoryerror-when-seemingly-unrelated-code-block-commented-out) – Sotirios Delimanolis Feb 17 '14 at 01:34

3 Answers3

4

Arrays are objects. When you assign them like this

int[][] firstArray = getArray();  // Get your array from somewhere
int[][] secondArray = firstArray; // Assign an array

you have only one array, but you have a second variable that references it. Essentially, secondArray[i][j] and firstArray[i][j] refer to the same object in memory by two different names.

To fix this, make a copy of the array. A simple way of doing it is to call the clone() method on the original array, like this:

int[][] secondArray = new int[firstArray.length][];
for (int i = 0 ; i != secondArray.length ; i++) {
    secondArray[i] = (int[])firstArray[i].clone();
}

Note: clone() creates a shallow copy. If you

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

My first idea was:

char[] orig = // something;
char[] copy = orig.clone();

But be aware: clone() only creates a shallow copy of an array. That means for a 2D-Array with chars you have to clone each subarray.

For further information about clone() click here.

Your code should look something like this

char[][] original = new char[3][3];
char[][] copy = new char[orignal.length][0];

for(int i = 0; i < original.length; i++){
    copy[i] = original[i].clone();
}
ROT13
  • 375
  • 2
  • 7
0

I have two identical 2D array's of characters however I want to make it so that when I make changes to one it doesn't effect the other. I am assuming it is because they are sharing the same space in memory and was wondering how to stop this.

Arrays don't share memory. Nor do any other variables in Java.

Ergo if changes to one affect the other, you don't have two arrays at all. You only have one, with two references to it. Updating the array via one reference is visible via the other reference.

The solution is therefore to create two arrays, not just one.

user207421
  • 305,947
  • 44
  • 307
  • 483