arraycopy example in Java

By: Charles Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The System class has an arraycopy method that you can use to efficiently copy data from one array into another:
public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)
The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.

The following program, ArrayCopyDemo, declares an array of char elements, spelling the word "decaffeinated". It uses arraycopy to copy a subsequence of array components into a second array:

class ArrayCopyDemo {
    public static void main(String[] args) {
        char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
			    'i', 'n', 'a', 't', 'e', 'd' };
        char[] copyTo = new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 7);
        System.out.println(new String(copyTo));
    }
}

The output from this program is:

caffein

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(18)


1. View Comment

That was nice.

View Tutorial          By: Arnab at 2007-10-17 02:02:11
2. View Comment

Great example - easy to understand!

View Tutorial          By: George at 2009-12-30 03:48:37
3. View Comment

Very good example! Thanks so much!

View Tutorial          By: Chrissy at 2010-04-14 22:37:56
4. View Comment

Helps very much. Thank you.

View Tutorial          By: Uwe at 2010-08-17 12:35:44
5. View Comment

very nice example

View Tutorial          By: lois at 2010-10-05 23:05:01
6. View Comment

Very very nice example thank you

View Tutorial          By: Fabrice at 2010-12-03 03:36:18
7. View Comment

nice effort

View Tutorial          By: Tiger at 2011-07-06 02:17:13
8. View Comment

very clear explanation. thank for sharing explanation about java arraycopy :)

View Tutorial          By: toto at 2011-07-18 01:04:56
9. View Comment

The arraycopy in a System is static native method not simply static method.....which will use machine instruction level code to copy, for better performance..............

View Tutorial          By: Ashwinkumar.vj at 2011-07-19 09:25:02
10. View Comment

Great work ;)

View Tutorial          By: Rizwanulla Sharieff at 2012-08-21 04:42:41
11. View Comment

Very Nice Example...!!thanks

View Tutorial          By: Shafique khan at 2013-01-22 18:58:22
12. View Comment

better if it is explained with output of the program

View Tutorial          By: dinesh at 2013-05-05 13:57:21
13. View Comment

thank you. its very useful :)

View Tutorial          By: Farhanna at 2013-05-22 05:33:46
14. View Comment

nice

View Tutorial          By: pran at 2013-07-16 05:03:19
15. View Comment

Nice example.
I got it finally
Thank you so much


View Tutorial          By: Bo at 2015-05-05 06:28:43
16. View Comment

Thanks, you save my day!

View Tutorial          By: ivex at 2015-06-25 20:20:07
17. View Comment

Nice example of System.arraycopy()function. Thank you so much :-)

View Tutorial          By: Rahul Raj at 2015-08-17 03:11:44
18. View Comment

how come its able to copy from copyFrom to CopyTo.i couldn't understand the above example.could any 1 explain

View Tutorial          By: sameera at 2015-09-12 13:56:29

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Charles
arraycopy example in Java
Action listeners in JSF
Automatically Including Preludes and Codas in JSP
Using realloc() Function in C
Handling multiple buttons in HTML Form in Struts
Fopen and Getc implementation program in C
Pointers to Structures example program in C
Pointers vs. Multi-dimensional Arrays in C
getch and ungetch in C
Bitwise Operators in C
Variables and Arithmetic Expressions in C
Differences between JavaServer Faces technology and Struts
assert() example program in C++
'double buffering' Sample program in J2ME
Converting Default Function Arguments in C++ to Java

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to use ArrayList in Java
InetAddress Example program in Java
FileReader and FileWriter example program in Java
Method Overloading (function overloading) in Java
Student marks calculation program in Java
Simple Port Scanner application using Java
Read from a COM port using Java program
Calculate gross salary in Java
delete() and deleteCharAt() in Java
append() in Java
indexOf( ) and lastIndexOf( ) in Java
XML and Java - Parsing XML using Java Tutorial
Vector in Java - Sample Program
Using PrintWriter in Java
Integer: byte, short, int, and long data types in Java
Most Emailed Articles (in Java)
Using Charts in JasperReports
Introducing Nested and Inner Classes
Increment and Decrement Operator
Tomcat and httpd configured in port 8080 and 80
List all the serial and parallel ports in your system using Java
What is Unicode?
StreamTokenizer sample program in Java
FileReader and FileWriter example program in Java
FilenameFilter - sample program in Java
Transient vs Volatile modifiers in Java
Basics of Inheritance in Java
Method Overloading (function overloading) in Java
while - Iteration in java
how to use boolean data type in Java
equals( ) Versus == in Java