-1

Possible Duplicate:
Make copy of array Java

I have a main method call of c3 = c1.replicate();

c1 = new Chromosome(new int[] {1, 2, 3, 4, 5, 6})

I am trying really hard to create a replicate method in java that will copy c1 and attach it to the c3 = call.

Any help would be awesome!

Community
  • 1
  • 1
user2027757
  • 13
  • 1
  • 6

2 Answers2

2

You can use System.arrayCopy() in @jsoft solution for copying array faster.

Bimalesh Jha
  • 1,464
  • 9
  • 17
1
public Chromosome replicate(){
  int [] newArray = new int[this.chromosomeArrayOrWhateverYouNamedIt.length];
  for(int i=0; i < this.chromosomeArrayOrWhateverYouNamedIt.length; i++){
     newArray[i]=this.chromosomeArrayOrWhateverYouNamedIt[i];
  }
  Chromosome retVal = new Chromosome(newArray);
  return retVal;
}

I think that should work for you.

djjolicoeur
  • 484
  • 3
  • 7