Sunday, March 09, 2008

How to use System class to copy Array

arrayCopy() method of System class copy data from one array into another. The arrayCopy method requires five arguments:

     public static void arraycopy(Object source, int srcIndex,
Object dest, int destIndex, int length)

The two Object arguments indicate the source and destination array. The three integer arguments indicate the starting location in each the source and the destination array, and the number of elements to copy.

This method will copy ‘length’ number of elements of array ‘source’ starts from the location ‘srcIndex’ to the array ‘dest’ starts from ‘destIndex’.

public class ArrayCopyDemo {

public static void main(String[] args) {

char[] Src = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] dst = new char[7];
System.arraycopy(src, 2, dst, 0, 7);
System.out.println(new String(dst));

}
}

No comments: