How to Get Subarray in Java? Last Updated : 09 Dec, 2024 Comments Improve Suggest changes 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. Comment More infoAdvertise with us Next Article How to Get Subarray in Java? J juhisrivastav Follow Improve Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read Like