File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Copy an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copy an ArrayList" Watch "Copy an ArrayList" New topic

Copy an ArrayList

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I got a Business Object and one member is a arraylist. I have a setmethod on that member that takes in an arraylist. I wanted to set that arraylist to the member arraylist. How do I get it to work?
ArrayList a;
ArrayList b;
I can say a = b.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Of course note that this doesn't copy the ArrayList object nor does it make a copy of the components of the ArrayList. It just assigns the ArrayList object to the member identifier.
If you wanted to make a copy of the ArrayList, the code you'd use would depend upon what you meant by 'copy'. If a shallow copy were desired, then making use of the clone method or the ArrayList constructor that takes a Collection as an argument would suffice. If a new ArrayList object populated with copies of the components from the original ArrayList were desired, then the code to accomplish this task could become considerably more involved.
Are you getting any ideas, Anthony?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Copy an ArrayList
It's not a secret anymore!