Review:    arrays in Java

  • An array in Java consists of 2 parts:

    1. An array reference variable
      (which is not an array, but a variable that references to an array object)

    2. The array object which consists of N consecutive variables

  • Schematically:

  What does it mean to copy a variable ?  

  • A copy of a variable has the following properties:

    • The copy and the (original) variable have the same value

    • The copy and the (original) variable are independent:

      • When the copy is update, the (original) variable remains unchanged and vice versa

    Example: variable xCopy is a copy of the variable x

       double x = 4;
    
       double xCopy;
    
       xCopy = x;       // xCopy is now a copy of x
    
       xCopy = 99;      // xCopy is update, x is unchanged
    

What does it mean to copy an array ?

  • Copy an array means:

      • Make a duplicate of an array where the duplicate contains the same data as the original

      • Updating array elements in the duplicate must not affect the data in the original array

  • Schematically: before making a copy of the array myList

What does it mean to copy an array

  • Copy an array means:

      • Make a duplicate of an array where the duplicate contains the same data as the original

      • Updating array elements in the duplicate must not affect the data in the original array

  • Schematically: after making a copy of the array myList

How to copy an array in Java    step-by-step instructions

  • Initial state:

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
           
            double[] myListCopy = new double[ myList.length ];
            
            for ( int i = 0; i < myList.length; i++ )
                myListCopy[i] = myList[i];
        } 

How to copy an array in Java    step-by-step instructions

  • Step 1: create an array to store the copy

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
          
            double[] myListCopy = new double[ myList.length ];
     
            // Note: all entries of array are 0 (zero) ! 
                
        } 

How to copy an array in Java    step-by-step instructions

  • Step 2: copy the array elements to the copy array

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
          
            double[] myListCopy = new double[ myList.length ];
     
            for ( int i = 0; i < myList.length; i++ )
                myListCopy[i] = myList[i];
        } 

DEMO: demo/08-array/06-copy-array/CopyArray.java        Demo in BleuJ

A common error in array copy

  • Variables of primitive data types (such as int, double, etc) can be copied with an assignment statement:

        int x = 4;
        int xCopy;
    
        xCopy = x;   // Copies x to xCopy 

  • However, in Java, the assignment statement will not copy objects of non-primitive data types:

        double[] myList = {34, 15, 66, 7};
    
        double[] myListCopy;
    
        myListCopy = myList;   // Does not copy an array object 
                               // ** This copies the reference
    			   // ** in myList to myListCopy

    We study what happens in the assignment myListCopy = myList in detail next

The effect of assignment using a reference variable

  • Consider the following array myList:

        double[] myList = {34, 15, 66, 7};
    
    
       
         

    The array is stored as follows inside the computer memory:

The effect of assignment using a reference variable

  • We first consider the effect of the definition:   double[] myListCopy:

        double[] myList = {34, 15, 66, 7};
    
        double[] myListCopy;
    
     

    This definition will define a reference variable:

The effect of assignment using a reference variable

  • The assignment myListCopy = myList copies the reference in myList to myListCopy:

        double[] myList = {34, 15, 66, 7};
    
        double[] myListCopy;
    
        myListCopy = myList;   // Copies the reference  

    Schematically:

The effect of assignment using a reference variable

  • Because myListCopy and myList reference the same array object, updates made to myListCopy[i] will also affect myList[i] and vice verse:

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
    
            double[] myListCopy;
    
            myListCopy = myList; // Does not copy an array object 
    
    	myListCopy[1] = 999; // Will also change myList[1] !!
    
    	myList[3] = 999;     // Will also change myListCopy[3] !!
        }  

DEMO: demo/08-array/06-copy-array/ArrayAlias.java        Demo in BlueJ

  Alias  

  • We saw in the last example that: myListCopy and myList were different names that refer to same the array object:

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
    
            double[] myListCopy;
    
            myListCopy = myList; // Does not copy an array object 
    
    	myListCopy[1] = 999; // Will also change myList[1] !!
    
    	myList[3] = 999;     // Will also change myListCopy[3] !!
        }  

  • Alias:

      • When different variable names refer to the same object, they are called aliases in Computer Science

Quiz:    what is printed by the following Java program

What is printed by this program:

    public static void main(String[] args)
    {
        int[] a = {1, 2, 3, 4};
        int[] b = {9, 8, 7, 6, 5};
    
        int[] x;
    
        x = a;  // x is now an alias for a

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints ???
        System.out.println();
        
        x = b;  // x is now an alias for b

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints ???
        System.out.println();
    }  

Quiz:    what is printed by the following Java program

Answer:

    public static void main(String[] args)
    {
        int[] a = {1, 2, 3, 4};
        int[] b = {9, 8, 7, 6, 5};
    
        int[] x;
    
        x = a;  // x is now an alias for a

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints 1 2 3 4
        System.out.println();
        
        x = b;  // x is now an alias for b

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints 9 8 7 6 5
        System.out.println();
    }  

DEMO: demo/08-array/06-copy-array/QuizAlias.java

Review:    assigning references and alias

The reason why the first for-loop prints the elements in array a is because x is an alias of a:

    public static void main(String[] args)
    {
        

        x = a;  // x is now an alias for a

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints a[ ]
        System.out.println();
        
        x = b;  // x is now an alias for b

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints b[ ]
        System.out.println();
    }  

Review:    assigning references and alias

The reason why the second for-loop prints the elements in array b is because x is an alias of b:

    public static void main(String[] args)
    {
        

        x = a;  // x is now an alias for a

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints a[ ]
        System.out.println();
        
        x = b;  // x is now an alias for b

        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");    // Prints b[ ]
        System.out.println();
    }