How to Get Subarray in Java? Last Updated : 09 Dec, 2024 Suggest changes Share Like Article Like Report In Java, subarrays are the contiguous portion of an array. Extracting subarrays in Java is common when working with data that needs slicing or partitioning. Java does not have a direct method to create subarrays, we can extract subarrays using simple techniques like built-in methods or manual loops. Example: The simplest way to get a subarray is by using a manual loop. This approach is straightforward and gives us full control over the array slicing. Java // Java Program to get a Subarray in Java // Using a Simple Loop public class SubArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // Define the range for the subarray int a = 1; // Start index (inclusive) int b = 4; // End index (exclusive) // Create the subarray using a loop int[] sub = new int[b - a]; for (int i = a; i < b; i++) { sub[i - a] = arr[i]; } System.out.print(""); for (int n : sub) { System.out.print(n + " "); } } } Output2 3 4 Other Methods to Get Sub Array1. Using Arrays.copyOfRange()The Arrays.copyOfRange() is the easiest and efficient method to get a subarray. It eliminates the need for a manual loop. It is useful when we want a quick and clean way to extract a subarray without need to think about index management. Java // Java Program to Get a Subarray // Using Arrays.copyOfRange() method import java.util.Arrays; public class SubArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // Extract subarray using copyOfRange // From index 1 to 4 (exclusive) int[] sub = Arrays.copyOfRange(arr, 1, 4); System.out.println("" + Arrays.toString(sub)); } } Output[2, 3, 4] 2. Using Java Streams (Java 8+)In Java 8, Streams provide a modern, functional approach for extracting subarrays. This method is useful when additional processing is needed. Java // Java Program to Get a Subarray // Using Java Streams import java.util.Arrays; import java.util.stream.IntStream; public class SubArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // Extract subarray using streams int[] sub = IntStream.range(1, 4) // From index 1 to 4 (exclusive) .map(i -> arr[i]) .toArray(); System.out.println("" + Arrays.toString(sub)); } } Output[2, 3, 4] Explanation: The IntStream.range(from, to) generates a stream of indices. The .map(i -> array[i]) maps each index to the corresponding array element. The .toArray() method converts the stream back to an array. Advertise with us Next Article How to Get Subarray in Java? J juhisrivastav Follow Similar Reads How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of 5 min read How to Get Last Element in Array in Java? In Java, to get the last element in an array, we can access the element at the index array.length - 1 using array indexing. The length property of the array provides its total size, and subtracting one from it gives the index of the last element.Example 1: Here, we will access the last element in an 2 min read Array getShort() method in Java The java.lang.reflect.Array.getShort() is an in-built method of Array class in Java and is used to return the element present at a given index from the specified Array as a short. Syntax: Array.getShort(Object []array,int index) Parameters: array: The object array whose index is to be returned. inde 3 min read How to get slice of a Primitive Array in Java Given a Primitive Array, the task is to get a Slice of this array in Java, using start and ending index. Examples: Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 2, endIndex = 4 Output: {3, 4, 5} Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 0, endIndex = 1 Output: {1, 2}Method 1: Naive Method.Get the 8 min read Array get() Method in Java The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Syntax Array.get(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned. in 3 min read Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java Like