Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copy an ArrayList

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic