Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Arrays.copyOfRange() vs System.arraycopy

Hi,

When to use below two functions. Are there special cases in which one is preferred to use over other?

Arrays.copyOfRange() and System.arraycopy


I was looking here

http://www.programcreek.com/2015/03/system-arraycopy-vs-arrays-copyof-in-java/ 

int[] copied = Arrays.copyOf(arr, 10); //10 the the length of the new array
System.out.println(Arrays.toString(copied));




copied = Arrays.copyOf(arr, 3); // are they truncating new array copied size from 10 to 3 in this step???
System.out.println(Arrays.toString(copied));
SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

System.arraycopy() vs. Arrays.copyOf() in Java
 

If we want to copy an array, we can use either System.arraycopy() or Arrays.copyOf(). In this post, I use a simple example to demonstrate the difference between the two.


1. Simple Code Examples

System.arraycopy()

int[] arr = {1,2,3,4,5};
 
int[] copied = new int[10];
System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy
 
System.out.println(Arrays.toString(copied));
Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 0, 0, 0, 0]
Arrays.copyOf()

int[] copied = Arrays.copyOf(arr, 10); //10 the the length of the new array
System.out.println(Arrays.toString(copied));
 
copied = Arrays.copyOf(arr, 3);
System.out.println(Arrays.toString(copied));
Output:

[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
[1, 2, 3]

i did not understand the difference of output w.r.t two above approaches clearly. please advise
Avatar of Kanti Prasad
Kanti Prasad

Hi

Ignore the 1st output of ten 0

Let us go through the 2nd output
[0, 1, 2, 3, 4, 5, 0, 0, 0, 0]

int[] copied = new int[10]; \\ Here copied array is defined with size 10

System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy
\\ here arraycopy is telling arr to leave the 1st one then copy arr values which are 1,2,3,4,5 from the 2nd element till the 5th element. Hence in the output 1st is 0 2nd to 5th is 1,2,3,4,5 and as copied array is size 10 the rest are 0
 
this just printed after the above instructions
System.out.println(Arrays.toString(copied));

Here is the syntax in English
System.arraycopy(array that has data, starting position, destination array, Starting position of the element, Length);


int[] copied = Arrays.copyOf(arr, 10); //Here arr array size is increased from 5 to 10

System.out.println(Arrays.toString(copied));

So the output is 1,2,3,4,5 plus as you increased the size to 10 it printed 5 plus additional 5 0 as now the size of arr is 10 and this size 10 is copied in the copied array
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]


Here
copied = Arrays.copyOf(arr, 3); //Here arr array size is reduced  from 5 to 3
System.out.println(Arrays.toString(copied));

So the output is 1,2,3, only as you decreased the size to 3 so  it printed on 3 as it only copied into the copied array with size as 3
[1, 2, 3, ]
Avatar of gudii9

ASKER

Indeed if you look at the code for "copyOf" you can see that it calls System.arracopy internally.
It just allocates a new array and then copies to it.

How to see the code?
Please advise
Avatar of gudii9

ASKER

import java.util.Arrays;


public class ArrayCopyOfSystemArrayCopy {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = {1,2,3,4,5};
		 
		int[] copied = new int[10];
		System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy
		 
		System.out.println(Arrays.toString(copied));
		
		
		int[] copied2 = Arrays.copyOf(arr, 10); //10 the the length of the new array
		System.out.println(Arrays.toString(copied2));
		 
		copied = Arrays.copyOf(arr, 3);
		System.out.println(Arrays.toString(copied));
	}

}

Open in new window


above gave below output
[0, 1, 2, 3, 4, 5, 0, 0, 0, 0]
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
[1, 2, 3]


when i pressed control and clicked
.copyOf

i got below source code which do have System.arraycopy


 public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];
        [b]System.arraycopy[/b](original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

Open in new window


But i wonder why i do not see as below as you mentioned

    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        // Doug: This is fairly complicated code but all it is doing is allocating a new array
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);

        // Doug: Then here we copy from the original array to this newly allocated array
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

Open in new window


please advise