HashMap storing int[], when I modify one array, all the arrays change?

Hi,

I get a lot of help from these forums, but I couldn't solve this one particular problem!

I have a HashMap<Integer, int[]> and int[] keeps a list of integers that represent gene structures of my traders or their genome.

I'm trying to change one different gene in every array, eg the first gene in the first array or genome, second gene in the second array etc..

However, as soon as i change the first array, all the other array gets modified as well and moreover, when the second one gets modified the first one gets modified again with all the other ones..

I can't figure out why, as the arrays are supposed to be distinct entities kept in different memory locations, so they shouldn't be affected?

Here is my piece of code that i wrote, if anyone can help out i would really appreciate.

int[] genome = createRandomGenome(); //this method randomly initializes my genome array with integers

LinkedHashMap<Integer, int[]> neighbours = new LinkedHashMap<Integer, int[]>(geneSize);

for(int i=0; i<geneSize; i++) {

neighbours.put(i, genome);

}

for(int i=0; i><geneSize; i++) {

int[] current = neighbours.get(i);

int gene = current;

if(i==0 || i==1) {

gene++;

gene = gene%4;

if(gene==0) gene = 4;

current = gene;

neighbours.put(i, current);

continue;

}

if(1><i && i><16) {

gene++;

gene = gene%5;

if(gene==0) gene = 5;

current = gene;

neighbours.put(i, current);

continue;

}

if(i==16 || i==17) {

gene++;

gene = gene%3;

if(gene==0) gene = 3;

current = gene;

neighbours.put(i, current);

continue;

}

}

[1817 byte] By [strangerousa] at [2007-11-27 0:29:10]
# 1

int[] genome = createRandomGenome();

for(int i=0; i<geneSize; i++) {

neighbours.put(i, genome);

}

Here is your problem. You made 1 array "genome", then every entry in your map is associated with the same array.

Instead, you need to do something like this:

for(int i=0; i><geneSize; i++) {

int[] genome = createRandomGenome();

neighbours.put(i, genome);

}

>

KathyMcDonnella at 2007-7-11 22:31:16 > top of Java-index,Core,Core APIs...
# 2
I see, so they are merely references to the old object. but then the createRandomGenome method will create different arrays for me.how can i keep the same array and copy it to others?Thanks.
strangerousa at 2007-7-11 22:31:16 > top of Java-index,Core,Core APIs...
# 3

> how can i keep the same array and copy it to others?

I assume you mean, how can you have the same initial values in these distinct arrays.

For fast array copy, use System.arraycopy (which uses native code for faster copy).

See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

Try

int[] genome = createRandomGenome();

for(int i=0; i<geneSize; i++) {

int[] temp = new int[genome.length];

System.arraycopy(genome, 0, temp, 0, genome.length);

neighbours.put(i, temp);

}

>

KathyMcDonnella at 2007-7-11 22:31:16 > top of Java-index,Core,Core APIs...
# 4
Thanks a lot, it works! :)
strangerousa at 2007-7-11 22:31:16 > top of Java-index,Core,Core APIs...