Get Subarray between Specified Indexes in Java3 min read

Before getting into the programming on subarray between specified indexes, let us first learn about the subarray.

What is a subarray?

For example, consider an array = [2,3,4], then the subarrays are [2], [3], [4], [2,3], [3,4], and [2,3,4]. But something like [2,4] would not be considered as subarray.

For an array of size n, we can calculate non-empty subarrays by n*(n+1)/2.


Get a subarray of an array between specified indexes in Java

The various methods to get java array subarray between specified indices is as follows:

1. Arrays.copyOfRange()

It is the standard way to get a subarray of an array that returns a subarray from the specified range of an original array. Following is the example:

int[] copyOfRange(int[] original, int from index number, int to index number)

Output:

[4, 5, 6, 7, 8, 9]


2. System.arraycopy()

This is also one of the methods that are used to copy from the specified position of the source array to the destination array.

Example of System.arraycopy():

Output:


3. Arrays.copyOf()

This is also another way of copying the specified array to a specified type of array. But if the sub-array is not at the first index but is at some intermediate index, this method won’t work.

Example of Arrays.copyOf():

Output:

The sub_array:
[A, B, C, D, E, F]


4. Custom Method

This is a method where we can write our own custom method to copy the specified elements from an array to the new array.

Example to demonstrate the Custom method:

Output:

The sub_array:
[D, E, F, G]


5. Subarray: without copying

Sometimes it may cause a problem in performance while copying the large part of an array. And in such cases, instead of locating a new array, we can use the instance of the same array.

Example of subarray through callback

Output:

The result:
4
5
6
7
8
9


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …