Wednesday, April 2, 2014

Copy Array

This exercise show various case in copy array. Imagine a case, I have a array of char, char[]. In each step, only one element of the array will be changed. And I have to record the content of the array in each step. So I create a queue of array of char to save the record. The pseudo code is like this:

- new a queue of array of char, Queue<char[]> myQueue = new LinkedList<char[]>().
- new a array of char with default value, myArray = new char[] {' ', ' ', ' '}.
- change the 1st char in myArray, and add it to myQueue.
- change the 2nd char in myArray, and add it to myQueue.
- change the 3rd char in myArray, and add it to myQueue.

Actually, it is not work! Because in Java, when we pass a array to a method, it pass the reference of the array. That means all the items in the queue point to the same object of array. The result is when we change any char in myArray, all items in myQueue will be changed.

The solution is to clone() to another array, or even create another array, before passing and add to the queue. The following code and out show the details.

package javatestarray;

import java.util.LinkedList;
import java.util.Queue;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestArray {
    
    class MyClass{
        
        Queue<char[]> myQueue;
        
        MyClass(){
            myQueue = new LinkedList<char[]>();
        }
        
        public void insert (char[] arrayIn){
            myQueue.add(arrayIn);
        }
        
        public void printAll(){
            while(!myQueue.isEmpty()){
                char[] removedItem = myQueue.remove();
                System.out.print(removedItem);
                System.out.println(" : " + removedItem.toString());
            }
        }
    }
    
    class MyAnotherClass{
        
        Queue<Character> myQueue;
        
        MyAnotherClass(){
            myQueue = new LinkedList<Character>();
        }
        
        public void insert (char charIn){
            myQueue.add(charIn);
        }
        
        public void printAll(){
            while(!myQueue.isEmpty()){
                Character removedItem = myQueue.remove();
                System.out.print(removedItem);
                System.out.println(" : " + removedItem.toString());
            }
        }
    }

    JavaTestArray(){
        MyClass myObj1 = new MyClass();
        char[] myArray1 = new char[] {' ', ' ', ' '};
        myArray1[0] = 'A';
        myObj1.insert(myArray1);
        myArray1[1] = 'B';
        myObj1.insert(myArray1);
        myArray1[2] = 'C';
        myObj1.insert(myArray1);
        myObj1.printAll();
        
        MyAnotherClass myAnotherObj = new MyAnotherClass();
        char myChar = 'A';
        myAnotherObj.insert(myChar);
        myChar = 'B';
        myAnotherObj.insert(myChar);
        myChar = 'C';
        myAnotherObj.insert(myChar);
        myAnotherObj.printAll();
        
        MyClass myObj2 = new MyClass();
        char[] myArray2 = new char[] {' ', ' ', ' '};
        myArray2[0] = 'A';
        myObj2.insert(myArray2.clone());
        myArray2[1] = 'B';
        myObj2.insert(myArray2.clone());
        myArray2[2] = 'C';
        myObj2.insert(myArray2.clone());
        myObj2.printAll();
        
        MyClass myObj3 = new MyClass();
        char[] myArray3 = new char[] {'A', ' ', ' '};
        myObj3.insert(myArray3);
        myArray3 = new char[] {'A', 'B', ' '};
        myObj3.insert(myArray3);
        myArray3 = new char[] {'A', 'B', 'C'};
        myObj3.insert(myArray3);
        myObj3.printAll();
        
        MyClass myObj4 = new MyClass();
        myObj4.insert(new char[] {'A', ' ', ' '});
        myObj4.insert(new char[] {'A', 'B', ' '});
        myObj4.insert(new char[] {'A', 'B', 'C'});
        myObj4.printAll();
    }
   
    public static void main(String[] args) {
        JavaTestArray myJavaTestArray = new JavaTestArray();
    }
    
}


No comments:

Post a Comment