I have an ArrayList
that has some objects that I defined in it. Based on some criteria, I want to copy some of the objects from the first list to the second list (which starts empty), but these objects that I copy could be copied several times ( can be duplicates ).
To simplify for an example let's say my second list contains only 2 objects that are duplicates. Objects a and b.
If I modify something in object a, will that modification also appear on object b ? It is just a reference that is passed or it's a copy of an object?
for( int i = 0; i < Selected.size(); i++)
{
double chance = random.nextInt(100);
chance = chance/100;
if( chance >= constCrossover )
{
Cross.add(Selected.get(i));
//here i add items that might be duplicates
}
}
I will make further modifications in the Cross list and i don't want duplicate objects to interact with eachcother.
It's always a reference. The value of an expression, the value in an array etc is always either a reference or a primitive value. Nothing will copy an object implicitly - you'd have to do that explicitly.
(As an aside, you should follow Java naming conventions - Selected
and Cross
should probably be selected
and cross
.)
See more on this question at Stackoverflow