Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Reassigning an array of a class.

Thread view: 
MS - 26 May 2005 18:12 GMT
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
Eric Sosman - 26 May 2005 18:33 GMT
> Hi,
>
[quoted text clipped - 20 lines]
>
> 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.

Signature

Eric.Sosman@sun.com

MS - 26 May 2005 19:10 GMT
>     Yes.  Neither `population' nor `nextGeneration' is
> actually an array; both are references to array objects.
[quoted text clipped - 8 lines]
> are no other references to the upper array, it becomes
> eligible for garbage collection.

Many thanks Eric.
Nigel Wade - 27 May 2005 12:38 GMT
>> Hi,
>>
[quoted text clipped - 33 lines]
> 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.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

MS - 28 May 2005 11:38 GMT
>>    Yes.  Neither `population' nor `nextGeneration' is
>>actually an array; both are references to array objects.
[quoted text clipped - 15 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.