6 Replies Latest reply on Dec 20, 2008 1:48 PM by John Hall

    Clone ArrayCollection?

    john isaacks Level 1
      I have a getter function inside my Singleton data class that returns an ArrayCollection but for the sake of encapsulation I would like to return a copy/clone of the ArrayCollection rather than the array collection itself...how can I do this?

      or would it be better to store it as an array, then in the getter function return this:
      return new ArrayCollection(the_array);
      or would this be passing a reference to the_array?

      Please advice

      Thanks!!
        • 1. Re: Clone ArrayCollection?
          Garyl Woolworth Level 1
          Either of these ways seem to work. I would test it though to make sure that updating the clone doesn't still reference the parent of where it came from but I would need more of an example of what it is you want to do to extend this.
          • 2. Re: Clone ArrayCollection?
            Level 7

            "john isaacks" <webforumsuser@macromedia.com> wrote in message
            news:gigts5$cd9$1@forums.macromedia.com...
            >I have a getter function inside my Singleton data class that returns an
            > ArrayCollection but for the sake of encapsulation I would like to return a
            > copy/clone of the ArrayCollection rather than the array collection
            > itself...how
            > can I do this?
            >
            > or would it be better to store it as an array, then in the getter function
            > return this:
            > return new ArrayCollection(the_array);
            > or would this be passing a reference to the_array?

            You would wind up with two ArrayCollections that both had the same source.
            I'm not sure this is such a bad thing, since it makes sure that the
            different copies of the data are synched.

            If you really need to do this, I think that there is an objectUtils class
            that will allow you to copy objects, so you can loop through an array and
            copy its objects. I'm not sure how well this works for deep/complex
            objects.

            HTH;

            Amy


            • 3. Re: Clone ArrayCollection?
              john isaacks Level 1
              quote:

              Originally posted by: Newsgroup User

              "john isaacks" <webforumsuser@macromedia.com> wrote in message
              news:gigts5$cd9$1@forums.macromedia.com...
              >I have a getter function inside my Singleton data class that returns an
              > ArrayCollection but for the sake of encapsulation I would like to return a
              > copy/clone of the ArrayCollection rather than the array collection
              > itself...how
              > can I do this?
              >
              > or would it be better to store it as an array, then in the getter function
              > return this:
              > return new ArrayCollection(the_array);
              > or would this be passing a reference to the_array?

              You would wind up with two ArrayCollections that both had the same source.
              I'm not sure this is such a bad thing, since it makes sure that the
              different copies of the data are synched.

              If you really need to do this, I think that there is an objectUtils class
              that will allow you to copy objects, so you can loop through an array and
              copy its objects. I'm not sure how well this works for deep/complex
              objects.

              HTH;

              Amy





              Well what if I did this instead:
              return new ArrayCollection(the_array.concat());

              Thanks.
              • 4. Re: Clone ArrayCollection?
                John Hall Level 4
                That can work under many bconditions but a better description is here:

                http://tinyurl.com/97frhl
                • 5. Re: Clone ArrayCollection?
                  john isaacks Level 1
                  quote:

                  Originally posted by: John Hall
                  That can work under many bconditions but a better description is here:

                  http://tinyurl.com/97frhl


                  Thanks, I had no idea about that only returning a shallow copy. I understand that if the array has an object inside it the new shallow copy will reference the same object. But what about something primitive like strings, if the array only has strings in it, it will not be a shallow copy right? Because a string can't have multiple reference. I am not sure?

                  Thanks.
                  • 6. Re: Clone ArrayCollection?
                    John Hall Level 4
                    You're correct. Strings, ints, uints etc do not copy by reference.