![]() |
The Advantage of ArrayList is it can be defined without giving a predefined size. But the disadvantage is it is more expensive to create and maintain. To have the solution for these expenses we can create a deep copy of an ArrayList. There are two types of copies that can be made the first one is a deep copy and the second one is a shallow copy. In this article, we will learn to make a deep copy of Java ArrayList in Java. PrerequisiteDeep Copy of ArrayListUnlike shallow copy in deep copy, we entirely make a new ArrayList without referring to the old ArrayList. In this, instead of duplicating the ArrayList it also copies the objects in it. When we make a deep copy we entirely make a new ArrayList at the new address and then copy the elements one by one. Therefore if we make any changes in one list it doesn’t get reflected in the second ArrayList. Following is the example of code in which we will make a deep copy of an ArrayList in Java. Java
Output:Original List: [1,2,3] Explaination of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |