System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
is a native method.
What is the time complexity for this method?
It's native , written in the language of the operating system, which means that the implementation of arraycopy() is platform dependant. So, in conclusion it's likely O(n), but maybe not. @HawkeyeParker, you can get the source code for a native method on a specific platform.
arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.
arraycopy() simply copies values from the source array to the destination, Arrays. copyOf() also creates new array. If necessary, it will truncate or pad the content.
System. arraycopy does shallow copy, which means it copies Object references when applied to non primitive arrays. Therefore after System.
It will have to go through all the elements in the array to do this. Array is a unique data structure where you have to specify a size when you initialize it. Order would be the source array's size or in Big O terms its O(length).
Infact this happens internally in an ArrayList. ArrayList wraps an array. Although ArrayList looks like a dynamically growing collection, internally it does an arrycopy when it has to expand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With