package com.mindprod.example;
import java.util.Arrays;
import static java.lang.System.*;
/**
* Methods to demonstrate the various ways of copying an array.
* <p/>
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2006-03-24
* @noinspection WeakerAccess
* @since 2006-03-24
*/
public final class TestArraycopy
{
/**
* Sample code to copy arrays four different ways.
*
* @param args not used.
*
* @noinspection ManualArrayCopy
*/
public static void main( String[] args )
{
final String[] planets = {
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
"Pluto" };
String[] planets1 = new String[ planets.length ];
for ( int i = 0; i < planets.length; i++ )
{
planets1[ i ] = planets[ i ];
}
out.println( ">>>planets1" );
for ( String planet : planets1 )
{
out.println( planet );
}
String[] planets2 = new String[ planets.length ];
System.arraycopy( planets, 0, planets2, 0, planets.length );
out.println( ">>>planets2" );
for ( String planet : planets2 )
{
out.println( planet );
}
String[] planets3 = Arrays.copyOf( planets, planets.length );
out.println( ">>>planets3" );
for ( String planet : planets3 )
{
out.println( planet );
}
String[] planets4 = Arrays.copyOfRange( planets, 0
, planets.length
);
out.println( ">>>planets4" );
for ( String planet : planets4 )
{
out.println( planet );
}
}
}