BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

ArrayList clone() method in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial, we will see examples of ArrayList clone() method. This method creates a shallow copy of an ArrayList.

Syntax:

newList = oldList.clone()

Creates a shallow copy of oldList and assign it to newList.

Example 1: Creating a copy of an ArrayList using clone()

In this example, we have an ArrayList of String type and we are cloning it to another ArrayList using clone() method. The interesting point to note here is that when we added and removed few elements from original ArrayList al after the clone() method, the another ArrayList al2 didn’t get affected. It shows that clone() method just returns a shallow copy of ArrayList.

import java.util.ArrayList;
public class Details {

   public static void main(String a[]){
     ArrayList<String> al = new ArrayList<String>();

     //Adding elements to the ArrayList
     al.add("Apple");
     al.add("Orange");
     al.add("Mango");
     al.add("Grapes");
     System.out.println("ArrayList: "+al);

     ArrayList<String> al2 = (ArrayList<String>)al.clone();
     System.out.println("Shallow copy of ArrayList: "+ al2);

     //add and remove from original ArrayList
     al.add("Fig");
     al.remove("Orange");

     //print both ArrayLists after add & remove
     System.out.println("Original ArrayList:"+al);
     System.out.println("Cloned ArrayList:"+al2);
   }
}

Output:

ArrayList: [Apple, Orange, Mango, Grapes]
Shallow copy of ArrayList: [Apple, Orange, Mango, Grapes]
Original ArrayList:[Apple, Mango, Grapes, Fig]
Cloned ArrayList:[Apple, Orange, Mango, Grapes]

Example 2: Clone Integer ArrayList

In the following example, we are creating a shallow copy of an Integer ArrayList using clone() method. This example is similar to the first example, except that here list contains integers.

import java.util.ArrayList;
public class JavaExample {

  public static void main(String args[])
  {
    // Creating an ArrayList
    ArrayList<Integer> numbers = new ArrayList<Integer>();

    // Adding elements to the arraylist
    numbers.add(3);
    numbers.add(7);
    numbers.add(5);
    numbers.add(11);
    numbers.add(9);

    // print first ArrayList
    System.out.println("Original ArrayList: "+ numbers);

    // Creating another ArrayList
    // Copying the array list "numbers" to this list
    ArrayList<Integer> copyList = (ArrayList<Integer>)numbers.clone();

    // Displaying the other linked list
    System.out.println("Second ArrayList: "+ copyList);
  }
}

Output:

Original ArrayList: [3, 7, 5, 11, 9]
Second ArrayList: [3, 7, 5, 11, 9]

Recommended Articles:

  • How to copy elements of one ArrayList to another ArrayList
  • When to use ArrayList and LinkedList in Java
  • How to increase the capacity of ArrayList
  • Perform binary search on ArrayList in Java
❮ Java Collections

Top Related Articles:

  1. Java ArrayList addAll(int index, Collection c) Method example
  2. How to Convert an array to ArrayList in java
  3. ArrayList in Java With Examples
  4. Java ArrayList addAll(Collection c) Method example
  5. Multilevel inheritance in java with example

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. reshma says

    June 17, 2017 at 8:03 AM

    Hello sir…The clone program doesn’t compile at all…
    when complied..It says..
    “Note: Java uses unchecked or unsafe operation ”
    ” Note: recompile with -Xlint: unchecked for details ”

    What could be the meaning of this ..How can I rectify it???

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap