Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Arraylist issue

  1. #1
    Bellyfish is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Arraylist issue

    Hey, having an issue with arraylists.

    I have some code which requires me to pass an arraylist into a method and then clear that arraylist.
    However whenever i clear it an empty arraylist is just passed into the method (which is understandable)

    Is there a way i can retain the arraylist data? Ive tried creating a new instance of the arraylist and making it equal to the old one but this is cleared when the old one is cleared. Is it possible to do even?

    (sorry if this is unclear, will provide code if this is gibberish)

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Arraylist issue

    You need to copy the content of the old ArrayList into the new one. If you just make the new one equal to the old ArrayList by using the assignment operator, you simply tell the new ArrayList to point to the old ArrayList. It will referring the same object. So whatever you do the the first ArrayList will affect the other.
    Bellyfish likes this.

  3. #3
    Bellyfish is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Arraylist issue

    Cheers for explaining it to me, was now able to create a new distinct arraylist.
    Thanks again!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,854
    Rep Power
    19

    Default Re: Arraylist issue

    ArrayList has a constructor that accepts another Collection (maybe just a List) as a parameter which copies the contents from the old one to the new one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 02-27-2012, 02:39 AM
  2. JAVA is kickin my butt - Arraylist issue
    By 2rr2nv in forum New To Java
    Replies: 1
    Last Post: 11-20-2011, 04:12 PM
  3. ArrayList issue
    By drogba123 in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 10:02 AM
  4. Replies: 19
    Last Post: 08-10-2010, 08:36 AM
  5. ArrayList issue
    By tomrobpowell in forum Eclipse
    Replies: 2
    Last Post: 05-08-2010, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •