I am attempting to copy all of the elements in an array into another one, however I am not grasping the concept of System.arraycopy(). Yes, I have researched into it and can't grasp the concept.
Asked
Active
Viewed 130 times
-1
-
Does this answer your question? [How does the System.arraycopy method work?](https://stackoverflow.com/questions/27517893/how-does-the-system-arraycopy-method-work) – Mihir Joshi Jul 21 '20 at 04:43
-
1I have already looked at that, but in my eyes that is copying one element to the array. – micgd Jul 21 '20 at 04:47
-
1Okay, I'll try to answer here, and try to be simpler. – Mihir Joshi Jul 21 '20 at 04:55
1 Answers
3
Parameters of arraycopy,
arraycopy(Object source, int sourceIndex, Object destination, int destinationIndex, int length);
An Example, Lets say we have 2 arrays each with 5 elements.
int arrayOne[] = {10,20,30,40,50}; // 5 Elements Each.
int arrayTwo[] = {2,4,6,8,10};
If we want to replace the 3rd element (2nd Index) of arrayTwo[2] with the 5th Element (4th Index) of arrayOne[4] we do the following.
System.out.println("Before (System.array.copy): "+arrayTwo[2]);
System.arraycopy(arrayOne,4,arrayTwo,2,1);
System.out.println("After (System.array.copy) : "+arrayTwo[2]);
The Output is as follows.
Before System.array.copy: 6
After System.array.copy : 50

ArbitraryChoices
- 243
- 1
- 11