Answers for "java copy arraylist to new arraylist"

1

copy arraylist java

List<Something> original=new ArrayList<>();
//insert elements
List<Something> copy=new ArrayList<>(original);//create copy here
System.out.println(copy.equals(original));//true because the copy has the same elements
System.out.println(copy==original);//false because it is another List
Posted by: Guest on April-15-2021
1

copy arraylist to another arraylist java

You can use such trick:

myObject = new ArrayList<Object>(myTempObject);
or use

myObject = (ArrayList<Object>)myTempObject.clone();
You can get some information about clone() method here

But you should remember, that all these ways will give you a copy of your List, not all of its elements. So if you change one of the elements in your copied List, it will also be changed in your original List.
Posted by: Guest on March-11-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language