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));
i was not clear on above point based on the example in the link?
please advise.
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));
The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arrayCopy copies into an existing array.
i was not clear on above point based on the example in the link?
please advise.