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 ) ;
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).
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 ;
}
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 ;
}
| 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' |
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 ;
}
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 ;
}
char [] arr = { 's', 'u', 'm', 'a', 't', 'r', 'a' } ;
char [] newArr ;
newArr = OurCopy.copySubarray( arr, 2, 3 ) ;