devxlogo

Copy One Array to Another Without Using Loops or Conditions

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]); }}
See also  5 Tips for Choosing the Right Medical Software for Your Practice
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist