Announcement

Collapse
No announcement yet.

List Array and DEEP copy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • List Array and DEEP copy

    Deep copy using the class Cloner
    PHP Code:
    http://code.google.com/p/cloning/ 
    Sample class
    PHP Code:
    package oggetto;

    public class 
    oggetto1 {
        public 
    int a 0;


    Main
    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).i;

            
    ArrayList<oggetto1array2= 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(0Double.valueOf(10));        // set a new double
                
    System.out.print(array.get(0));       // print the double

            ////////////////////////////////////////////////////////////


        
    }




    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio
Working...
X

Google Profile


My name is Giovanni Di Grezia, but people call me xgiovio.

Here is my homepage:.

I'm a VFX Artist and Software Developer.

Giovanni Di Grezia