HOWTO · Java
How to Slice an Array in Java
This article will help you in slicing an array into a subarray in Java.
On this page
To slice a Java array, copy the elements in a contiguous range into a new array. The examples below use a start-inclusive, end-exclusive range: fromIndex is included and toIndex is the first index left out. For example, the range [2, 5) selects indexes 2, 3, and 4. The original array remains unchanged.
Slice an Array in Java by Duplicating Elements
A loop makes the index mapping explicit. Allocate the result with length toIndex - fromIndex, then copy each selected value into the corresponding position in the result.
This complete program selects indexes 1 through 3 from an integer array:
import java.util.Arrays;
public class ArraySliceLoop {
static int[] slice(int[] values, int fromIndex, int toIndex) {
int[] result = new int[toIndex - fromIndex];
for (int i = 0; i < result.length; i++) {
result[i] = values[fromIndex + i];
}
return result;
}
public static void main(String[] args) {
int[] values = {10, 20, 30, 40, 50};
int[] part = slice(values, 1, 4);
System.out.println(Arrays.toString(part));
}
}
The result is a separate array. The i loop counter starts at zero for the result, while fromIndex + i locates the matching element in the source. Call the method with indexes that describe a valid range within the source array.
Slice an Array in Java Using the copyOfRange() Method
Arrays.copyOfRange() provides a direct way to make a new array from a range. The method belongs to java.util.Arrays; its from argument is inclusive and its to argument is exclusive. The exact API name includes copyOfRange().
This program copies indexes 1, 2, and 3:
import java.util.Arrays;
public class ArraySliceCopyOfRange {
public static void main(String[] args) {
int[] values = {10, 20, 30, 40, 50};
int[] part = Arrays.copyOfRange(values, 1, 4);
System.out.println(Arrays.toString(part));
}
}
The returned array contains the selected values, and changing it does not replace elements in the original array. For primitive arrays such as int[], the values are copied. For arrays of objects, the array is new but its elements are references to the same objects.
Slice an Array in Java Using a Stream
A stream can select array values by mapping a stream of indexes back to the source array. Treat this as an optional approach: use it only when the Java runtime configured for your project supports the required stream APIs. No runtime version is assumed here.
The operation can be written as IntStream.range(fromIndex, toIndex).map(i -> values[i]).toArray(). IntStream.range uses an inclusive start and exclusive end, so it follows the same range convention as the other methods. This compact expression is an illustration; verify API availability against your project’s declared Java runtime before using it. A loop or Arrays.copyOfRange() is a straightforward alternative when that runtime is unknown.
Slice an Array in Java Using the System.arraycopy Method
System.arraycopy copies a specified number of elements from one array into another. Create a destination array of length toIndex - fromIndex, then pass the source position, destination position, and copy length.
import java.util.Arrays;
public class ArraySliceSystemArraycopy {
public static void main(String[] args) {
int[] values = {10, 20, 30, 40, 50};
int fromIndex = 1;
int toIndex = 4;
int[] part = new int[toIndex - fromIndex];
System.arraycopy(values, fromIndex, part, 0, part.length);
System.out.println(Arrays.toString(part));
}
}
The arguments are the source array, source position, destination array, destination position, and number of elements to copy. Here the destination position is zero, so the selected source range fills part from its first position. The source and destination must have compatible array element types, and the positions and length must fit both arrays.
Slice an Array in Java Using asList and subList
For reference-type arrays, Arrays.asList() creates a fixed-size list backed by the array. subList(fromIndex, toIndex) returns a view of the selected list range. Convert that view to an array when the result should be a separate array.
import java.util.Arrays;
import java.util.List;
public class ArraySliceSubList {
public static void main(String[] args) {
String[] values = {"red", "green", "blue", "gold"};
List<String> list = Arrays.asList(values);
List<String> selected = list.subList(1, 3);
String[] part = selected.toArray(new String[0]);
System.out.println(Arrays.toString(part));
}
}
The list returned by Arrays.asList() has a fixed size: you can replace an element, but you cannot add or remove list entries. The sublist is a view tied to that list. Calling toArray() creates a separate array, although object elements themselves are still references to the same objects. This approach is for reference-type arrays such as String[]; primitive arrays need a primitive-array method such as the loop, copyOfRange(), or System.arraycopy shown above.
Conclusion
Use a loop when you want to see or customize the element-by-element copy. Choose Arrays.copyOfRange() for a direct array copy, or System.arraycopy when you want to specify the destination position and copy length. A stream offers index-based selection when the project’s Java runtime supports the needed APIs. For reference-type arrays, asList and subList provide a list view; call toArray() to get a separate result array.