Saturday, 9 May 2015

How To Shallow Copy Or Clone a ArrayList

How To Shallow Copy Or Clone  a ArrayList

Here we are creating duplicate object of an ArrayList instance . clone() Returns a shallow copy of this ArrayList instance.

import java.util.ArrayList;

public class CloneOfArrayList {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        // print arraylist
        System.out.println("arrayList is = " + arr);
        //Returns a shallow copy of this ArrayList instance
       ArrayList<String> clone1 = (ArrayList<String>) arr.clone();
       System.out.println("Clone is="+clone1);



arrayList is = [c, php, html, java]

Clone is=[c, php, html, java

Related Posts: 


Storing Java Object In ArrayList

No comments:

Post a Comment