Java ArrayListclone() Method
Example
Create a copy of a list:
import java.util.ArrayList;public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); ArrayList cars2 = (ArrayList)cars.clone(); cars2.set(0, "Toyota"); System.out.println(cars); System.out.println(cars2); }}Definition and Usage
Theclone() method returns a copy of theArrayList as anObject.
This creates a "shallow" copy, which means that copies of objects in the list are not created, instead the list has references to the same objects that are in the original list.
Note: Since the return type isObject, it must betype casted in order to use it as anArrayList as shown in the example above.
Syntax
public Object clone()Technical Details
| Returns: | A copy of theArrayList object. |
|---|
Related Pages
❮ ArrayList Methods

