Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Deep copy of ArrayList?

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Deep copy of ArrayList?

    Does:
    array.add(data.get(y));
    make a deep or shallow copy? I am trying to get an arrayList to collect data but iterating though an algorithm and then place that data into a new arrayList three times. Each new array has if (i = 0), if (i=1) etc around it, but they all have the same data at the end! (only Integer objects).

    I have been writing code for 2 days now, please feel free to point out the obvious.

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Deep copy of ArrayList?

    Assuming that data and array are both ArrayLists, and y is some index, then the same Object will be in both ArrayLists. You can test this just by using the == operator on the references returned by the get() methods of each ArrayList.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Deep copy of ArrayList?

    But is it a deep copy? As when I replace the data in the first arrayList (array) then both the arrayLists change. (That or I have another error)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Deep copy of ArrayList?

    What happened when you tried? It shouldn't be too hard to throw together a test program that tests this out. I'm really not even sure what you're asking- you mean if you modify the Object that's in both ArrayLists? Or if you add a new Object to one of the ArrayLists?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Copy Constructor
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2021, 09:12 PM
  2. How to copy J2ME application to a mobile device
    By vishal21 in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: January 21st, 2012, 05:52 PM
  3. how to copy cells to a new sheet of excel with JexcelAPI??
    By 19world in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: June 15th, 2010, 03:49 AM
  4. Copy folder based on latest date
    By ayonsoni in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 17th, 2010, 07:01 AM
  5. program a button that copy whatever in a JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 5
    Last Post: May 17th, 2010, 12:43 AM