[java-dev] perils of referencing
Ritchie Argue
maxmspjit at gmail.com
Thu Feb 7 14:05:43 MST 2008
- Previous message: [java-dev] perils of referencing
- Next message: [java-dev] Re: perils of referencing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Couldn't you say
ArrayList<Thing> getThings() { return Things.clone(); }
Then at least you have a copy that you can mess with in your
Thingulator, and removing items from there shouldn't delete them in
your ComplexThing. You don't really want a deep copy do you, just a
shallow copy of the actual ArrayList so you can mess with it in the
Thingulator, while leaving the management of the Thing object
lifetimes to the Things array in ComplexThing? Your sinking feeling
should be easy to verify with a small test class..
r.
On 7-Feb-08, at 1159, jbmaxwell wrote:
>
> Hello All,
>
> I need to clear something up...
>
> Suppose I have two classes: ComplexThing, and Thing.
>
> ComplexThing is a composite object, made up of Things, which has
> some special powers as a result (night vision, omnipotence, etc.).
> It has a simple variable, Things, which is an ArrayList<Thing>, and
> holds all Things that make up ComplexThing.
>
> ComplexThing has a method, getThings(), which returns all the Things
> from which it's made:
>
> ArrayList<Thing> getThings() {return Things;}
>
> I have another class, Thingulator, which operates on ComplexThings.
>
> if Thingulator does something like:
>
> ComplexThing bigThing;
> Thing aThing;
>
> ArrayList<Thing> theseThings = bigThing.getThings();
>
> if(theseThings.contains(aThing))
> theseThings.remove(aThing);
>
> Obviously, aThing gets removed from theseThings, but does it also
> get removed from ComplexThings.Things? I have a sinking feeling it
> does, as this makes perfect sense, given the whole idea of
> referencing, but that would also kind of suck if it did. I think
> there are a few places where I've done this sort of "removal",
> without realizing how far-reaching the references are...
>
> Also, if my fears are well-founded, is the only way around it a deep
> copy of ComplexThings.Things? I'm guessing so, but that also kind of
> sucks, as deep copies in Java are such a pain (why has this not been
> made simpler, over the years?).
>
> Any clarification welcome.
>
> cheers,
>
> J.
>
>
> _______________________________________________
> java-dev mailing list
> java-dev at cycling74.com
> http://www.cycling74.com/mailman/listinfo/java-dev
- Previous message: [java-dev] perils of referencing
- Next message: [java-dev] Re: perils of referencing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
