trying to switch beginning of an array with another
Posted
by
user1874574
on Stack Overflow
See other posts from Stack Overflow
or by user1874574
Published on 2012-12-04T05:01:55Z
Indexed on
2012/12/04
5:03 UTC
Read the original article
Hit count: 105
I have a problem where i am trying to swap two arrays so that they switch beginnings.
example: array 1 = (1,2,3,4,5,6,7,8) and array 2 = (11,12,13,14,15,16,17,18) i want to end up with the first array being (11,12,13,14,5,6,7,8) and i want the second array to be (1,2,3,4,15,16,17,18)
but for some reason i end up with 1=(11,12,13,14,5,6,7,8) and 2=(11,12,13,14,15,16,17,18)
my code is provided below, what am i doing wrong?
public static void Mutate(Genetic lowest, Genetic secondLowest) {
int halfway = (lowest.getPopulation().length)/2;
int[] one = lowest.getPopulation();
int[] two = secondLowest.getPopulation();
int[] temp = secondLowest.getPopulation();
int[] temp2 = lowest.getPopulation();
for(int i = 0; i < halfway; i++){
temp[i] = one[i];
}
lowest.setPopulation(temp);
for(int i = 0; i < lowest.getPopulation().length; i++){
System.out.print(temp[i]);
}
System.out.println();
for(int i = 0; i < halfway; i++){
temp2[i] = two[i];
}
for(int i = 0; i < lowest.getPopulation().length; i++){
System.out.print(temp2[i]);
}
}
© Stack Overflow or respective owner