How to create a sub-array from another array? Is there a method that takes the indexes from the first array such as:
methodName(object array, int start, int
Using Apache ArrayUtils downloadable at this link you can easy use the method
subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive)
"boolean" is only an example, there are methods for all primitives java types
Use copyOfRange method from java.util.Arrays class:
int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);
For more details:
Link to similar question
You can use
Arrays.copyOfRange(Object[] src, int from, int to)
Javadoc
System.arraycopy(Object[] src, int srcStartIndex, Object[] dest, int dstStartIndex, int lengthOfCopiedIndices);
Javadoc
int newArrayLength = 30;
int[] newArray = new int[newArrayLength];
System.arrayCopy(oldArray, 0, newArray, 0, newArray.length);
Yes, it's called System.arraycopy(Object, int, Object, int, int) .
It's still going to perform a loop somewhere though, unless this can get optimized into something like REP STOSW
by the JIT (in which case the loop is inside the CPU).
int[] src = new int[] {1, 2, 3, 4, 5};
int[] dst = new int[3];
System.arraycopy(src, 1, dst, 0, 3); // Copies 2, 3, 4 into dst
I you are using java prior to version 1.6 use System.arraycopy()
instead. Or upgrade your environment.