-1

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.

micgd
  • 51
  • 7

1 Answers1

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