Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Arrays copyOfRange() Method

In this tutorial, we will learn about the copyOfRange() method in Arrays class in Java. This method is very much similar to the copyOf() method but it also provides more flexibility in terms of range i.e. it receives the starting index and ending index of an existing array to create a new array.

Syntax

public static datatype[] copyOfRange(datatype[] existing_array, int start_index, int end_index)

This method will receive an existing_array from which we want to create a new array, start_index from which we will copy array and end_index up to which we will copy the array and finally it will return a newly created array.

List of Overloading Methods of copyRange() Method

This table contains all the overloaded variants of copyOfRange() method.

Method Description

static boolean[] copyOfRange(boolean[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static byte[] copyOfRange(byte[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static char[] copyOfRange(char[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static double[] copyOfRange(double[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static float[] copyOfRange(float[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static int[] copyOfRange(int[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static long[] copyOfRange(long[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static short[] copyOfRange(short[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static <T> T[] copyOfRange(T[] original, int from, int to)

This method copies the specified range of the specified array into a new array.

static <T,?U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)

This method copies the specified range of the specified array into a new array.

enlightenedWhile passing start_index it should not be less than 0, on the other hand, end_index can exceed the length of the array. In a case, we pass end_index greater than the length of an existing array all then those elements will be assigned as zero (0).

Example of copyOfRange() when end index is less than the length of an array

In this example, we are creating new_array from an existing array arr using copyOfRange() method. In our example, arr will consist of all the elements from the index 3 to 11.

import java.util.Arrays; 
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = {17, 32, 44, 99, 29, 22, 5, 18, 51, 66, 12, 14, 57}; 
		int[] new_array = Arrays.copyOfRange(arr, 3, 11); 
		for (int num : new_array) 
			System.out.print(num + " "); 

	} 
} 


99 29 22 5 18 51 66 12

Example of copyOfRange() when end index exceeds the length of an array

In the example given below, the ending index is passed as 10 but in the existing array maximum possible index is 5 so all the later index elements will be assigned as a zero (0).

import java.util.Arrays; 
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = {17, 32, 44, 99, 29, 22}; 
		int[] new_array = Arrays.copyOfRange(arr, 3, 10); 
		for (int num : new_array) 
			System.out.print(num + " "); 

	} 
} 


99 29 22 0 0 0 0

Example of copyOfRange() for an array of objects

In the example given below, we created an array of objects of Student class, and then using copyRangeOf() method we created a new array new_array. This example illustrates how we can use this method for creating an array of objects also.

import java.util.Arrays;
class Student
{
	String stud_name;
	int roll_no;
	Student(String stud_name, int roll_no)
	{
		this.stud_name = stud_name;
		this.roll_no = roll_no;
	}
}
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		Student[] arr = new Student[4];		
		arr[0] = new Student("ABC",123);
		arr[1] = new Student("DEF",456);
		arr[2] = new Student("GHI",963);		
		Student[] new_arr = Arrays.copyOfRange(arr,0,3);
		for(Student obj:new_arr){
			System.out.println("Name: "+obj.stud_name+" Roll No: "+obj.roll_no);
		}
	} 
} 


Name: ABC Roll No: 123
Name: DEF Roll No: 456
Name: GHI Roll No: 963

Example of all the overloaded methods of copyRange() Method

In the example given below, we implemented all the overloaded method examples in one place. Here arrays of different data types are given and we are creating new arrays of specific range from an existing array using copyOfRange() method.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		//Example of static boolean[]	copyOfRange(boolean[] original, int from, int to)
		boolean[] array1={true, false, true, false, false, true};
		boolean[] new_arr1=Arrays.copyOfRange(array1, 0, 6);
		System.out.println("New Boolean Array: "+Arrays.toString(new_arr1));

		//Example of static byte[]	copyOfRange(byte[] original, int from, int to)
		byte[] array2={4, 1, 8, 2, 2, 3, 16, 4, 4};
		byte[] new_arr2=Arrays.copyOfRange(array2, 0, 9);
		System.out.println(Arrays.toString(new_arr2));

		//Example of static char[]	copyOfRange(char[] original, int from, int to)
		char[] array3={'a', 'e', 'i', 'o', 'u'};
		char[] new_arr3=Arrays.copyOfRange(array3, 0, 5);
		System.out.println(Arrays.toString(new_arr3));

		//Example of static double[]	copyOfRange(double[] original, int from, int to)
		double[] array4={12, 3, 17, 5, 8, 17, 2, 37};
		double[] new_arr4=Arrays.copyOfRange(array4, 0, 8);
		System.out.println(Arrays.toString(new_arr4));

		//Example of static float[]	copyOfRange(float[] original, int from, int to)
		float[] array5={4.12f, 7.3f, 8.17f, 1.5f, 7.8f, 4.17f, 3.2f, 6.37f};
		float[] new_arr5=Arrays.copyOfRange(array5, 0, 8);
		System.out.println(Arrays.toString(new_arr5));

		//Example of static int[]	copyOfRange(int[] original, int from, int to)
		int[] array6 ={5, 6, 7, 10, 17};		
		int[] new_arr6=Arrays.copyOfRange(array6, 0,5);
		System.out.println(Arrays.toString(new_arr6));

		//Example of static long[]	copyOfRange(long[] original, int from, int to)
		long[] array7={4,54,56,584,51,84,5,28,33,9};
		long[] new_arr7=Arrays.copyOfRange(array7, 0,10);
		System.out.println(Arrays.toString(new_arr7));

		//Example of static short[]	copyOfRange(short[] original, int from, int to)
		short[] array8={10, 8, 6, 4, 2, 0};
		short[] new_arr8=Arrays.copyOfRange(array8, 0,6);
		System.out.println(Arrays.toString(new_arr8));
	}
}


New Boolean Array: [true, false, true, false, false, true]
[4, 1, 8, 2, 2, 3, 16, 4, 4]
[a, e, i, o, u]
[12.0, 3.0, 17.0, 5.0, 8.0, 17.0, 2.0, 37.0]
[4.12, 7.3, 8.17, 1.5, 7.8, 4.17, 3.2, 6.37]
[5, 6, 7, 10, 17]
[4, 54, 56, 584, 51, 84, 5, 28, 33, 9]
[10, 8, 6, 4, 2, 0]

Conclusion:

In this tutorial, we learned how to create a new array from an existing array using the copyOfRange() method. This method is the same as copyOf() method but this accepts a start index and end index for copying a sliced array.