Copy One Array to Another Without Using Loops or Conditions

Here’s the code the usage of arraycopy:

public class testarrcopy{ public static void main(String arg[]) {   //Array of any object can be user defined object   String source[] ={"one","two","thr","four","five","six","sev","eight","nine","ten"};   //Array of any object can be user defined object   String dest[] = new String[5];   ...some conditions or calculations..   /*    *This method copies source array to destination Array.    *It starts copy from the position 3 from source array.    *If we didn't properly set the values to the function it may leads tothe exceptions like     *IndexOutOfBoundsException,ArrayStoreException,NullPointerException    */   System.arraycopy(source,3,dest,0,5);   //the following loop tests the values in the dest array   for(int i=0; i< dest.length; i++)     System.out.println(dest[i]); }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: