arraycopy example in Java

By: Charles  

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:


Archived Comments

1. i want to serch an element from original array and put it into new array ,also i want to put a v
View Tutorial          By: pooja at 2016-08-06 18:07:19

2. How it is possible to display if we copy from one integer array to another integer array.pls reply
View Tutorial          By: Renju at 2016-05-17 16:39:20

3. What does 7 indicates? .. In
system.arraycopy (copyfrim, 2,copyto,7 )

View Tutorial          By: Preetam at 2015-12-20 10:55:39

4. how come its able to copy from copyFrom to CopyTo.i couldn't understand the above example.could any
View Tutorial          By: sameera at 2015-09-12 13:56:29

5. Nice example of System.arraycopy()function. Thank you so much :-)
View Tutorial          By: Rahul Raj at 2015-08-17 03:11:44

6. Thanks, you save my day!
View Tutorial          By: ivex at 2015-06-25 20:20:07

7. T̍̃̔̉̉̑̐&Ig
View Tutorial          By: Bob The Zealot at 2015-05-26 03:44:45

8. Nice example.
I got it finally
Thank you so much

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

9. nice
View Tutorial          By: pran at 2013-07-16 05:03:19

10. thank you. its very useful :)
View Tutorial          By: Farhanna at 2013-05-22 05:33:46

11. better if it is explained with output of the program
View Tutorial          By: dinesh at 2013-05-05 13:57:21

12. Very Nice Example...!!thanks
View Tutorial          By: Shafique khan at 2013-01-22 18:58:22

13. Great work ;)
View Tutorial          By: Rizwanulla Sharieff at 2012-08-21 04:42:41

14. This is a very good example and easy to understand
View Tutorial          By: Sandaru at 2012-08-19 11:07:23

15. Thanx for giving a good example for System.arraycopy()...
View Tutorial          By: Tincy at 2012-07-19 04:20:36

16. The arraycopy in a System is static native method not simply static method.....which will use machin
View Tutorial          By: Ashwinkumar.vj at 2011-07-19 09:25:02

17. very clear explanation. thank for sharing explanation about java arraycopy :)
View Tutorial          By: toto at 2011-07-18 01:04:56

18. nice effort
View Tutorial          By: Tiger at 2011-07-06 02:17:13

19. nice effort
View Tutorial          By: Tiger at 2011-07-06 02:16:56

20. Really useful
View Tutorial          By: Vignesh at 2011-03-11 05:24:40

21. Very very nice example thank you
View Tutorial          By: Fabrice at 2010-12-03 03:36:18

22. very nice example
View Tutorial          By: lois at 2010-10-05 23:05:01

23. Helps very much. Thank you.
View Tutorial          By: Uwe at 2010-08-17 12:35:44

24. Very good example! Thanks so much!
View Tutorial          By: Chrissy at 2010-04-14 22:37:56

25. Great example - easy to understand!
View Tutorial          By: George at 2009-12-30 03:48:37

26. That was nice.
View Tutorial          By: Arnab at 2007-10-17 02:02:11

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial