Hi i'm using the clone() method to copy the reference of my arraylist like this
import java.util.*;
public class Cloning {
public static void main(String[] args) {
ArrayList v = new ArrayList();
for(int i = 0; i < 10; i++ )
v.add(new Int(i));
System.out.println("v: " + v);
ArrayList v2 = (ArrayList)v.clone();
for(int i=0;i<v2.size();i++ )
((Int)v2.get(i)).increment();
System.out.println("v: " + v);
}
}
The output is
v: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Otherwise if i make this
import java.util.*;
public class Cloning {
public static void main(String[] args) {
ArrayList v = new ArrayList();
for(int i = 0; i < 10; i++ )
v.add(new Int(i));
System.out.println("v: " + v);
ArrayList v2 = v;
for(int i=0;i<v2.size();i++ )
((Int)v2.get(i)).increment();
System.out.println("v: " + v);
}
}
The output is the same. So my question is, if i use the clone() method for arraylist or i do ArrayList v2 = v; is the same thing?