I am pretty sure you can use copyOfRange(double[] original, int from, int to)
from java.util.Arrays
. @Rolfl said, and I quote : It is recommended practice to use Arrays.copyOf instead of System.arraycopy().
If you need to cut up the million+ array for parallel processing, then I would suggest you chunk the data while reading it, this should save you a processing step.
Edit: Internally, Arrays.copyOf()
uses System.arraycopy()
to do the hard work of duplicating the Array data in the new copy, and it will make your code look like this simple 1-liner:
double[] firstArray = Arrays.copyOf(arrayToCut, 50);
double[] secondArray = Arrays.copyOfRange(arrayToCut, 50, 100);
While using System.arraycopy()
may seem like the fastest alternative, whenever you copy an array you have to:
- first create the destination for the data to be copied to. This step requires initializing all the destination array points to their initialization values, which is a linear operation, but done in native code, so is fast.
- then call the System.arraycopy() for the actual data re-write.
There are many ways that people may want to do this operation, and the various Arrays.copy*()
methods are designed to make the relatively mundane, yet still rather complicated and bug-prone process a lot simpler, and less buggy.
Whenever you have a use case where you are copying data from one array to a new array, you should by default use the Arrays.copy*()
methods. Only when you have a use case where you are overwriting data in to an existing array (or relocating data inside an array) should you use System.arraycopy()
. As for the performance aspect, it appears that the performance of most native mechanisms are similar.
Arrays.copy* methods lead to better, more maintainable, and less buggy code, and, there is essentially no performance penalty because it is just doing the work you were going to do anyway.