arraycopy example in Java

By: Charles Viewed: 161306 times  Printer Friendly Format    


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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

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

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

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

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

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

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

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

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

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

10. very clear explanation. thank for sharing explanat
View Tutorial          By: toto at 2011-07-18 01:04:56

11. The arraycopy in a System is static native method
View Tutorial          By: Ashwinkumar.vj at 2011-07-19 09:25:02

12. Thanx for giving a good example for System.arrayco
View Tutorial          By: Tincy at 2012-07-19 04:20:36

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

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

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

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

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

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

19. Nice example.
I got it finally
Thank

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

20. TÌÌÌÌÌ&Igr
View Tutorial          By: Bob The Zealot at 2015-05-26 03:44:45

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

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

23. how come its able to copy from copyFrom to CopyTo.
View Tutorial          By: sameera at 2015-09-12 13:56:29

24. What does 7 indicates? .. In
system.array

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

25. How it is possible to display if we copy from one
View Tutorial          By: Renju at 2016-05-17 16:39:20

26. i want to serch an element from original array a
View Tutorial          By: pooja at 2016-08-06 18:07:19