I have an interface DataSeries with a method
int[] getRawData();
For various reasons (primarily because I’m using this with MATLAB, and MATLAB handles int[] well) I need to return an array rather than a List.
I don’t want my implementing classes to return the int[] array because it is mutable. What is the most efficient way to copy an int[] array (sizes in the 1000-1000000 length range) ? Is it clone()?
The only alternative is
Arrays#copyOf()(which usesSystem#arrayCopy()under the hoods).Just test it.
and when
test1()andtest2()are swapped:The difference is negligible. I’d say, just go for
clone()since that is better readable andArrays#copyOf()is Java 6 only.Note: actual results may depend on platform and JVM used, this was tested at an Dell Latitude E5500 with Intel P8400, 4GB PC2-6400 RAM, WinXP, JDK 1.6.0_17_b04