Java Arrays copyOfRange() Method



Description

The Java Arrays copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) method Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to).

Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from. The resulting array is of the class newType.

Declaration

Following is the declaration for java.util.Arrays.copyOfRange() method

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

Parameters

  • original − This is the array from which a range is to to be copied.

  • from − This is the initial index of the range to be copied, inclusive.

  • to − This is the final index of the range to be copied, exclusive.

  • newType − The class of the copy to be returned

Return Value

This method returns a new array containing the specified range from the original array, truncated or padded with nulls to obtain the required length

Exception

  • ArrayIndexOutOfBoundsException − If from < 0 or from > original.length()

  • IllegalArgumentException − If from > to.

  • NullPointerException − If original is null.

  • ArrayStoreException − If an element copied from original is not of a runtime type that can be stored in an array of class newType.

Copying an Array of Objects to a Same Sized Array Example

The following example shows the usage of Java Arrays copyOfRange() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with same size is created using copyOfRange() method and printed.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) };

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArr.length; i++) {
         System.out.print(studentArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of same size
      Monitor[] studentArrCopy = Arrays.copyOfRange(studentArr, 0, 3, Monitor[].class);

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArrCopy.length; i++) {
         System.out.print(studentArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

class Monitor extends Student{

   boolean priviledges;

   Monitor(int rollNo, String name, boolean priviledges) {
      super(rollNo, name);
      this.priviledges = priviledges;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]
Copied Arrays: 
Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]

Copying an Array of Objects to a Higher Sized Array Example

The following example shows the usage of Java Arrays copyOfRange() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with higher size is created using copyOfRange() method and printed with default values appended.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) };

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArr.length; i++) {
         System.out.print(studentArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of greater size
      Monitor[] studentArrCopy = Arrays.copyOfRange(studentArr, 1, 5, Monitor[].class);

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArrCopy.length; i++) {
         System.out.print(studentArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

class Monitor extends Student{

   boolean priviledges;

   Monitor(int rollNo, String name, boolean priviledges) {
      super(rollNo, name);
      this.priviledges = priviledges;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]
Copied Arrays: 
Student Array: [[ 2, Robert, true ] [ 3, Adam, false ] null null ]

Copying an Array of Objects to a Lower Sized Array Example

The following example shows the usage of Java Arrays copyOfRange() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with lower size is created using copyOfRange() method and printed.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) };

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArr.length; i++) {
         System.out.print(studentArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of lesser size
      Monitor[] studentArrCopy = Arrays.copyOfRange(studentArr, 1, 2, Monitor[].class);

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArrCopy.length; i++) {
         System.out.print(studentArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

class Monitor extends Student{

   boolean priviledges;

   Monitor(int rollNo, String name, boolean priviledges) {
      super(rollNo, name);
      this.priviledges = priviledges;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]
Copied Arrays: 
Student Array: [[ 2, Robert, true ] ]
java_util_arrays.htm
Advertisements