# 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`

### 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 …

### String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

### Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

### Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

### Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

### Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …