Deep copy using the class Cloner
Sample class
Main
PHP Code:
http://code.google.com/p/cloning/
PHP Code:
package oggetto;
public class oggetto1 {
public int a = 0;
}
PHP Code:
import java.util.ArrayList;
import com.rits.cloning.Cloner;
import oggetto.*;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 03/10/13
* Time: 22.15
*/
public class test {
public static void main(String[] input){
int i;
Cloner x_cloner = new Cloner();
ArrayList<oggetto1> array= new ArrayList<oggetto1>(); // create a list of oggetto1
for(i=0;i<10;++i)
array.add(new oggetto1); //////////// fill the list with 10 elements
array.get(i).a = i;
ArrayList<oggetto1> array2= new ArrayList<oggetto1>(); // create a new list of oggetto1
for(int i=0;i<array.size();++i)
array2.add(x_cloner.deepClone(array.get(i))); // hard copy elements from old list
///////////// now two arrays are deep copied
array.get(0).a=10; //assign a value to variable a into 1 object of first array
array2.get(0).a=20; //assign a value to variable a into 1 object of second array
System.out.println(array.get(0).a);
System.out.println(array2.get(0).a);
////////// 2 values are different. So the arrays contains different objects
///////////////////////////////////////////////example of Wrapper - array of Double
ArrayList<Double> array= new ArrayList<Double>();
array.add(new Double(0)); // add a double initialized to 0
System.out.print(array.get(0).doubleValue()); // print the double
array.set(0, Double.valueOf(10)); // set a new double
System.out.print(array.get(0)); // print the double
////////////////////////////////////////////////////////////
}
}