Thursday, November 23, 2017

JAVA - How to copy array values between arrays

For copy array values between two arrays use System.arraycopy():
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 this 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

No comments:

Post a Comment