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;
}
}

