• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overwriting arrays problem  RSS feed

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code to shuffle an array of integers around and the algorithm itself works fine.

The code works fine for creating chromosome 1 - which is an arry of integers. However, when creating the chromosome 2 - another array of itnegers, it somehow overwrites the first chromosome and I can't understand why.

Starting with the array int[] chromosomeEncodingForAlgorithms with each
element being a single number:

The output is:


Any ideas?
[ February 19, 2006: Message edited by: Sam Bluesman ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that what you are creating when you assign chromosone2 to chromosomeEncodingForAlgorithms is an alias to an array and not a new array. When you assign chromosone1 to that array, you also are creating an alias to the same array. Whatever you do with any alias is going to affect the actual object. So if you print out the elements using any alias, you are going to get the same thing.
[ February 19, 2006: Message edited by: Keith Lynn ]
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah cheers. Sorted now. Thanks for that
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey sam,

i see you are working hard on your issue. respect!

sorry, i did not dig deep into your snippet. are you sure it compiles? you have some strange method calls in it, and some semicolons are missing...

even though it is always good to play around and learn - don't re-invent the wheel. pls have a look at the following code snippet:



i'm using the shuffle() method of Collections. I'm also working with a List (the array is just for starting and easy initializing of the example).

i hope this is of some use for you,
jan
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!