Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Copying Arrays 
 

In this section, you will learn how to copy the elements of one array to another through an example.

 

Copying Arrays

                         

After learning all about arrays, there is still one interesting thing left to learn i.e. copying arrays. It means to copy data from one array to another. The precise way to copy data from one array to another is

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

Thus apply system's arraycopy method for copying arrays.The parameters being used are :-

src              the source array
srcIndex     start position (first cell to copy) in the source array
dest            the destination array
destIndex   start position in the destination array
length         the number of array elements to be copied

The following program, ArrayCopyDemo(in a .java source file), uses arraycopy to copy some elements from the copyFrom array to the copyTo array.

public class ArrayCopyDemo{
  public static void main(String[] args){
    char[] copyFrom = {'a','b','c','d','e','f','g','h','i','j'};
    char[] copyTo = new char[5];
    System.arraycopy(copyFrom, 2, copyTo, 05);
    System.out.println(new String (copyTo));
  }
}

Output of the program:

C:\tamana>javac ArrayCopyDemo.java
C:\tamana>java ArrayCopyDemo
cdefg
C:\tamana>

In this example the array method call begins the copy of elements from element number 2. Thus the copy begins at the array element 'c'. Now, the arraycopy method takes the copied element and puts it into the destination array. The destination array begins at the first element (element 0) which is the destination array copyTo. The copyTo copies 5 elements : 'c', 'd', 'e', 'f', 'g'. This method will take "cdefg" out of "abcdefghij", like this : 

Following image illustrates the procedure of copying array from one to another.

   

Donwload this example.

                         

» View all related tutorials
Related Tags: java c string arrays com array class strings object io objects type command new int id ai define for example

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.