Skip to content
Related Articles

Related Articles

Java.util.Arrays.copyOfRange() in Java

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Apr, 2022

This method creates a copy of elements, within a specified range of the original array.
Syntax : 
 

public static int[] copyOfRange(int[] original_array, int from_index, int to_index) 
  • original_array : Array to be copied from
  • from_index : Starting index of range to be copied
  • to_end : Ending index of range to be copied

Important points to remember while working with this method : 

  1. The initial index i.e(from_index) must lie within 0 to original_array.length
  2. The final index i.e(to_index) can be greater than or equal to length of original array. In case it is greater than the original Array then 0’s is assigned to the copy whose index is greater than or equal to original.length – from.
  3. Return array has length of will be to-from
  4. Its a method of Arrays class

Exceptions 

  • ArrayIndexOutOfBoundsException : This is thrown if the initial index i.e(from_index) 
    is out of range of original array
  • IllegalArgumentException : This is thrown if form_index>to_index
  • NullPointerException : This is thrown if the original array is null

Variations: 

copyOfRange(boolean[] original, int from, int to)
copyOfRange(byte[] original, int from, int to)
copyOfRange(char[] original, int from, int to)
copyOfRange(double[] original, int from, int to)
copyOfRange(float[] original, int from, int to)
copyOfRange(int[] original, int from, int to)
copyOfRange(long[] original, int from, int to)
copyOfRange(short[] original, int from, int to)
copyOfRange(T[] original, int from, int to)
copyOfRange(U[] original, int from, int to, Class newType)

Let’s understand this by an example: 

Java




// Java program to illustrate
// copyOfRange method
import java.util.Arrays;
class GFG {
public static void main(String args[])
    {
        int arr[] = { 12, 13, 14, 15, 16, 17, 18 };
 
        // to index is within the range
        int[] copy = Arrays.copyOfRange(arr, 2, 6);
        for (int i : copy)
            System.out.print(i + "  ");
 
        System.out.println();
        // to index is out of range
        // It assigns Zero to all the index out of range
        int[] copy1 = Arrays.copyOfRange(arr, 4, arr.length + 3);
 
        for (int i : copy1)
            System.out.print(i + "  ");
 
        // It throws IllegalArgumentException
        // int[] copy2 = Arrays.copyOfRange(arr, 5, 3);
 
        // It throws ArrayIndexOutOfBoundsException
        // int[] copy2 = Arrays.copyOfRange(arr, 10, arr.length + 5);
    }
}

Output: 
 

14  15  16  17  
16  17  18  0  0  0  

Lets see an Example for copyOfRange(T[] original, int from, int to) and copyOfRange(U[] original, int from, int to, Class newType) 
 

Java




// Java program to illustrate
// copyOfRange method
import java.util.Arrays;
class GFG {
    // User defined class
    static class Employee {
        int Eid;
        String Ename;
 
        // constructor
    public Employee(int Eid, String Ename)
        {
            this.Eid = Eid;
            this.Ename = Ename;
        }
 
        // Override toString()
    public String toString()
        {
            return Eid + "   " + Ename;
        }
    }
 
    public static void
    main(String args[])
    {
        Employee[] e = { new Employee(10, "geek1"),
                         new Employee(20, "geek2"),
                         new Employee(30, "geek3"),
                         new Employee(40, "geek4"),
                         new Employee(50, "geek5") };
 
        // Illustration of copyOfRange(T[] original, int from, int to)
        // Working with user defined class
        Employee[] getCopy_Employees = Arrays.copyOfRange(e, 3, 5);
        for (Employee e1 : getCopy_Employees)
            System.out.print(e1.toString() + "  ");
 
        System.out.println();
 
        // Illustration of copyOfRange(U[] original, int from, int to, Class newType)
        // In this we store the user defined class Employee into Object class
        // It Throws ArrayStoreException when we try to copy it in a class
        // That is not correct
        Object getcopy[] = Arrays.copyOfRange(e, 1, 3, Object[].class);
 
        // This throws an error
        // Number getcopy[] = Arrays.copyOfRange(e, 1, 3, Number[].class);
 
        for (Object e1 : getcopy) {
            System.out.print(e1.toString() + "  ");
        }
    }
}

Output: 
 

40   geek4  50   geek5  
20   geek2  30   geek3  
 

copyOf vs copyOfRange
copyOf() starts copying form the 0 th index of the original array and copies the specified number of elements 
While copyOfRange() can copy the range of elements form the original array.
Reference : 
docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOfRange-int:A-int-int-
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!