471,316 Members | 1,056 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

Reassigning an array of a class.

MS
Hi,

In my Genetic Algorithm program, I have a class called Genome. Another
class called GA has a class variable called 'population' which is an array
of type Genome.

One of the methods in GA, called CreateNextGeneration(), creates a local
array of class Genome called 'nextGeneration', and at the end of that
method I want the class variable 'population' to hold the array
'nextGeneration'.

Is it as simple as?

population = nextGeneration;

At the moment class Genome contains a copy method, and I have a loop which
copies all the Genomes in 'nextGeneration' to 'population' (the 2 arrays
are always the same size). This is obviously ineffecient, but I was
worried about what memory and garbage collection overheads there would be
it I used the line:

population = nextGeneration;

Should using this line be ok?

Many thanks,

MS
Jul 18 '05 #1
4 4234


MS wrote:
Hi,

In my Genetic Algorithm program, I have a class called Genome. Another
class called GA has a class variable called 'population' which is an array
of type Genome.

One of the methods in GA, called CreateNextGeneration(), creates a local
array of class Genome called 'nextGeneration', and at the end of that
method I want the class variable 'population' to hold the array
'nextGeneration'.

Is it as simple as?

population = nextGeneration;

At the moment class Genome contains a copy method, and I have a loop which
copies all the Genomes in 'nextGeneration' to 'population' (the 2 arrays
are always the same size). This is obviously ineffecient, but I was
worried about what memory and garbage collection overheads there would be
it I used the line:

population = nextGeneration;

Should using this line be ok?


Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.

--
Er*********@sun.com

Jul 18 '05 #2
MS
> Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.


Many thanks Eric.
Jul 18 '05 #3
Eric Sosman wrote:


MS wrote:
Hi,

In my Genetic Algorithm program, I have a class called Genome. Another
class called GA has a class variable called 'population' which is an array of type Genome.

One of the methods in GA, called CreateNextGeneration(), creates a local
array of class Genome called 'nextGeneration', and at the end of that
method I want the class variable 'population' to hold the array
'nextGeneration'.

Is it as simple as?

population = nextGeneration;

At the moment class Genome contains a copy method, and I have a loop which copies all the Genomes in 'nextGeneration' to 'population' (the 2 arrays
are always the same size). This is obviously ineffecient, but I was
worried about what memory and garbage collection overheads there would be
it I used the line:

population = nextGeneration;

Should using this line be ok?


Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.


One important point though, is that after 'population = nextGeneration' both
population and nextGeneration refer to the *same* array.

If your CreateNextGeneration() method simply "populates" the array
nextGeneration, then you will also change the data which population refers
to. You need to make sure that CreateNextGeneration() creates a new array.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nm*@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Jul 18 '05 #4
MS
>> Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.

One important point though, is that after 'population = nextGeneration' both
population and nextGeneration refer to the *same* array.

If your CreateNextGeneration() method simply "populates" the array
nextGeneration, then you will also change the data which population refers
to. You need to make sure that CreateNextGeneration() creates a new array.


Thanks.

CreateNextGeneration() does create a new local array, nextGeneration, and
fills it with new Genomes. It does some stuff and then finally reassigns
population: 'population = nextGegeration;'.

Cheers,

MS
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Joost Ronkes Agerbeek | last post: by
6 posts views Thread by Buddy Ackerman | last post: by
4 posts views Thread by songkv | last post: by
20 posts views Thread by bhalicki | last post: by
6 posts views Thread by woger151 | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.