how-to

Arrays

How to copy array values between arrays

For copy array values between two arrays use System.arraycopy() function:

String[] array = new String[3];

array[0] = "One";
array[1] = "Two";
array[2] = "Three";

String[] array1 = { "1", "2", "3" };

System.arraycopy( array1, 1, array, 0, 2 );

for (int i = 0; i < array.length; i++ )
  System.out.println( array[i] );

In the example are copied values of array1 (first param) from index 1 (second param) into array (third param). First value is inserted into 0 index (fourth param), copied are two (fifth param) items.

The output is:

2
3
Three