Java ArrayList copy
Yes, assignment will just copy the value of l1
(which is a reference) to l2
. They will both refer to the same object.
Creating a shallow copy is pretty easy though:
List<Integer> newList = new ArrayList<>(oldList);
(Just as one example.)
How to Duplicate an array list in Java?
Use
List<Movie> newList = new ArrayList<>(existingList);
Java ArrayList independent copy
Yes, this method should pass-by-reference (which list 'a' and 'copy' should be dependent). But these two operations don't prove this.
copy.set(0, "B");
copy.remove(copy.size()-1);
See if the following code helps you understand:
public static void main(String[] args) {
Process process = new Process(1);
Process process2 = new Process(2);
ArrayList<Process> a = new ArrayList<>(Arrays.asList(process, process2));
ArrayList<Process> copy = new ArrayList<>(a);
copy.get(0).id = 10;
// This proves that both ArrayLists maintain the same Process object at this point
// output:
// [Id:10, Id:2]
// [Id:10, Id:2]
System.out.println(a);
System.out.println(copy);
// copy.remove(copy.size() - 1) or copy.set(0, process3) doesn't affect another ArrayList
Process process3 = new Process(3);
process3.id = 100;
copy.set(0, process3);
copy.remove(copy.size() - 1);
// output:
// [Id:10, Id:2]
// [Id:100]
System.out.println(a);
System.out.println(copy);
}
static class Process {
public int id;
public Process(int id) {
this.id = id;
}
@Override
public String toString() {
return "Id:" + id;
}
}
How do I copy the contents of one ArrayList into another?
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.
how to deep copy one arraylist into another
As I mentioned in my comments to your question, the clone()
method returns an Object, and since the compiler can see and know only this, you must (dangerously) cast the object returned to be ArrayList for the method to work:
arrli.add((ArrayList<Integer>) arrli1.clone());
Having said this, I implore you not to use clone()
in this situation and that you read Why people are so afraid of using clone() (on collection and JDK classes)? to see why.
If this were my code, I'd make new ArrayLists objects within the for loop rather than cloning existing ones. I'm not 100% sure of what you're trying to achieve here, but perhaps something similar to:
// list to hold other lists
List<List<Integer>> listOfLists = new ArrayList<>();
// inner list to be added to above list
List<Integer> innerList = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
// if we need to create a new inner list, add old to
// list of lists and create new one
if (i != 0 && arr[i] < arr[i - 1]) {
listOfLists.add(innerList);
innerList = new ArrayList<>();
}
// always add to the current inner list
innerList.add(arr[i]);
}
// when all is done, add last inner list to list of lists
listOfLists.add(innerList);
This above code looks to be cleaner and safer. Also, if you're wondering why I'm using:
List<Integer> foo = new ArrayList<>();
rather than
ArrayList<Integer> foo = new ArrayList<>();
Please check out:
- Why do some people use the List base class to instantiate a new ArrayList? as well as
- What does it mean to “program to an interface”?
Related Topics
Java: Difference Between the Setpreferredsize() and Setsize() Methods in Components
How to Get a List of Dates Between Two Dates in Java
Best Way to List Files in Java, Sorted by Date Modified
Why Can't Strings Be Mutable in Java and .Net
"Code Too Large" Compilation Error in Java
Why Does the Division of Two Integers Return 0.0 in Java
Spring: @Component Versus @Bean
Using Env Variable in Spring Boot's Application.Properties
Is There a Destructor for Java
Where Is Array's Length Property Defined
Intellij Inspection Gives "Cannot Resolve Symbol" But Still Compiles Code
Multiple Wildcards on a Generic Methods Makes Java Compiler (And Me!) Very Confused
Get a Resource Using Getresource()
Can Overridden Methods Differ in Return Type
Java Regex Meta Character (.) and Ordinary Dot
Why Does the Tostring Method in Java Not Seem to Work for an Array