code_junkie

How do I clone a generic List in Java?

Nov 10th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. public Object clone()
  2.  
  3. ArrayList newArrayList = (ArrayList) oldArrayList.clone();
  4.  
  5. List<String> strs;
  6. ...
  7. List<String> newStrs = new ArrayList<String>(strs);
  8.  
  9. //assume oldList exists and has data in it.
  10. List<String> newList = new ArrayList<String>();
  11. Collections.copy(newList, oldList);
  12.  
  13. ArrayList(String) copy = new ArrayList(String)();
  14. copy.addAll(original);
  15.  
  16. ArrayList<String> orig = new ArrayList&<String>();
  17. ArrayList<String> copy = (ArrayList<String>) orig.clone()
  18.  
  19. ArrayList first = new ArrayList ();
  20. ArrayList copy = (ArrayList) first.clone ();
Add Comment
Please, Sign In to add comment