Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 24, 2003

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 to
the 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]);

 }
}
Elayaraja David
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap