Incremental Java
Copying Arrays

Copying Arrays

One of the most common things you can do with an array is to copy an array. We're going to copy an entire an array, and to copy part of an array.

Copying an array allows us to modify the copy without changing the original array.

We'll copy an array by implementing the following static method:

static int [] copyArray( int [] orig ) ;

Implementation of copyArray

We want to copy an array, but since arrays are objects, we must construct a new array with the same length as the original array.
int [] copyArray( int [] orig ) 
{
   // Construct array
   int [] newArr = new int[ orig.length ] ;
}
The following is incorrect code:
static int [] copyArray( int [] orig ) 
{
   // Construct array
   int [] newArr = new int[ orig.length ] ;

   // ERROR! Copies handles, not the array
   newArr = orig ;
}
The code above is incorrect. In particular, we constructed a new array of the same length as the original array. However, when we do the assignment newArr = orig, the orig handle is copied to newArr, and so both newArr and orig now have a handle to the orig array object.

The handle to newArr has been dropped, and so that array is now garbage (and is collected by the garbage collector).

Correct Way to Copy

Here's how to copy
static int [] copyArray( int [] orig ) 
{
   // Construct array
   int [] newArr = new int[ orig.length ] ;

   // Copy element by element using for loop
   for ( int i = 0 ; i < newArr.length ; i++ )
   {
      newArr[ i ] = orig[ i ] ;
   }

   return newArr ;
}

Error Checking

We should do some error checking. Any object handle can be null meaning that it doesn't hold a handle to a balloon. We can test for this, and then return null if that was passed in.
static int [] copyArray( int [] orig ) 
{
   // Do a null check
   if ( orig == null )
      return null ;

   // Construct array
   int [] newArr = new int[ orig.length ] ;

   // Copy element by element using for loop
   for ( int i = 0 ; i < newArr.length ; i++ )
   {
      newArr[ i ] = orig[ i ] ;
   }

   return newArr ;
}

Copying a Subarray

A subarray is part of an array (or possibly all of it). For example, suppose we have the following character array.

Index 0 1 2 3 4 5 6
Value 's' 'u' 'm' 'a' 't' 'r' 'a'

We want to implement the following method (assume it's in a class called OurCopy).

static char [] 
copySubarray( char [] orig, int srcIndex, int length ) ;
(I wrote the method on two lines so it would fit on the screen. Java permits you to write method signatures on two or more lines too).

This copies elements from orig starting at srcIndex and copies length elements.

For example, if you had:

  char [] arr = { 's', 'u', 'm', 'a', 't', 'r', 'a' } ;
  char [] newArr ;

  newArr = OurCopy.copySubarray( arr, 2, 3 ) ;
Then, newArr is an array with length 3, and is a copy of the subarray starting at index 2. newArr contains:

Index 0 1 2
Value 'm' 'a' 't'

Implemeting copySubarray

static char [] copySubarray( char [] orig, int srcIndex, int length ) 
{
   // Notice we create an array of size, length,
   // which is a parameter, not orig's length
   char [] newArr = new char[ length ] ;

   // Add more code
}
Next, we need to copy.
static char [] copySubarray( char [] orig, int srcIndex, int length ) 
{
   char [] newArr = new char[ length ] ;
   for ( int i = 0 ; i < length ; i++ )
   {
      // Add more code
   }
We've just created an array with length elements, so it makes sense to copy that many elements. The for loop iterates length times.

But what do we copy? We want to start copying from orig starting at srcIndex, but we want to copy to the new array starting at 0.

This is how it looks:

static char [] copySubarray( char [] orig, int srcIndex, int length ) 
{
   char [] newArr = new char[ length ] ;
   for ( int i = 0 ; i < length ; i++ )
   {
      newArr[ i ] = orig[ srcIndex + i ]
   }
   return newArr ;
}

Error Checking

There are several errors we could check for. We'll do the first two checks. You do the last one. In the last one, copy length elements or to the end of the array, whichever is shorter.

For the first two checks, we'll return null if the errors occur.

static char [] copySubarray( char [] orig, int srcIndex, int length ) 
{
   // Error check orig for null, or length for negative values
   if ( arr == null || length < 0 )
      return null ;

   char [] newArr = new char[ length ] ;
   for ( int i = 0 ; i < length ; i++ )
   {
      newArr[ i ] = orig[ srcIndex + i ]
   }
   return newArr ;
}

Tracing Code

Trace the following code to make sure you understand what's going on:
  char [] arr = { 's', 'u', 'm', 'a', 't', 'r', 'a' } ;
  char [] newArr ;

  newArr = OurCopy.copySubarray( arr, 2, 3 ) ;