🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
ArrayList.clone() method in Java is used to create a shallow copy of an ArrayList. This guide will cover the method's usage, explain how it works, and provide examples using List as the reference type to demonstrate its functionality.Table of Contents
- Introduction
cloneMethod Syntax- Examples
- Cloning an ArrayList
- Modifying the Cloned List
- Real-World Use Case
- Conclusion
Introduction
The clone() method is part of the ArrayList class in Java, and it creates a shallow copy of the list. This means that the elements themselves are not cloned, but the structure of the list is copied. Using List as the reference type ensures flexibility and compatibility with different list implementations.
clone Method Syntax
The syntax for the clone method is as follows:
public Object clone()
- The method returns a shallow copy of the
ArrayListinstance.
Examples
Cloning an ArrayList
The clone method can be used to create a copy of an existing ArrayList.
Example
import java.util.ArrayList;
import java.util.List;
public class CloneExample {
public static void main(String[] args) {
List<String> originalList = new ArrayList<>();
originalList.add("Apple");
originalList.add("Banana");
originalList.add("Orange");
// Clone the original list
List<String> clonedList = (List<String>) ((ArrayList<String>) originalList).clone();
System.out.println("Original List: " + originalList);
System.out.println("Cloned List: " + clonedList);
}
}
Output:
Original List: [Apple, Banana, Orange]
Cloned List: [Apple, Banana, Orange]
Modifying the Cloned List
Changes to the cloned list do not affect the original list and vice versa, as the clone is a shallow copy.
Example
import java.util.ArrayList;
import java.util.List;
public class ModifyClonedListExample {
public static void main(String[] args) {
List<String> originalList = new ArrayList<>();
originalList.add("Apple");
originalList.add("Banana");
originalList.add("Orange");
// Clone the original list
List<String> clonedList = (List<String>) ((ArrayList<String>) originalList).clone();
// Modify the cloned list
clonedList.add("Grapes");
System.out.println("Original List: " + originalList);
System.out.println("Cloned List after modification: " + clonedList);
}
}
Output:
Original List: [Apple, Banana, Orange]
Cloned List after modification: [Apple, Banana, Orange, Grapes]
Real-World Use Case
Backup and Restore of Data
In scenarios where you need to create a backup of the current state of a list and restore it later, the clone method is useful.
Example
import java.util.ArrayList;
import java.util.List;
public class BackupRestoreExample {
public static void main(String[] args) {
List<String> currentList = new ArrayList<>();
currentList.add("Task1");
currentList.add("Task2");
currentList.add("Task3");
// Create a backup of the current list
List<String> backupList = (List<String>) ((ArrayList<String>) currentList).clone();
// Modify the current list
currentList.add("Task4");
System.out.println("Current List: " + currentList);
// Restore the list from the backup
currentList = backupList;
System.out.println("Restored List: " + currentList);
}
}
Output:
Current List: [Task1, Task2, Task3, Task4]
Restored List: [Task1, Task2, Task3]
Conclusion
The ArrayList.clone() method in Java provides a convenient way to create a shallow copy of an ArrayList. By understanding how to use this method, you can efficiently manage the contents of your lists in Java applications, particularly when you need to create backups or temporary copies of your lists. Using List as the reference type in the examples ensures flexibility and compatibility with different list implementations. The clone method offers a straightforward and effective solution for copying lists.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment