Arrays copyOfRange() method in java

Let’s learn Arrays copyOfRange() method in java.

Arrays copyOfRange() method in java

Arrays copyOfRange() method in java copies specified range of the original array into a new array.

Also read – java arrays

The initial index of the range (from) must lie between 0 and original.length, inclusive.

Syntax:

public static int[] copyOfRange(int[] original_array, int from_index, int to_index)

Throws:

ArrayIndexOutOfBoundsException – if from < 0 or from > original.length.

IllegalArgumentException – if from > to.

NullPointerException – if original is null.

Note:

  • initial index (from) should lie between 0 and original.length (inclusive).
  • value at original is placed to the initial element of copy. Except for from == original.length or from == to.
  • final index (to) should be greater than or equal to from. In some cases it could be greater than original.length in this case 0 is placed in all elements of copy whose index is >= original.length (from).
  • length of returned array will be to – from.

Also read – arrays.sort java


Let’s see arrays copyOfRange() method in java example and source code.

import java.util.Arrays;
public class ArrayCopyOfRangeDemo 
{
   public static void main(String[] args) 
   {
      int[] arrNumber = { 66, 67, 68, 69, 70, 71, 72 };
      System.out.println("Given array: ");
      for(int a = 0; a < arrNumber.length; a++) 
      {
         System.out.println(arrNumber[a]); 
      }
      int[] copyNum = Arrays.copyOfRange(arrNumber, 2, 6);
      System.out.println("----------------------");
      System.out.println("Between range 2 and 6: ");
      for(int a : copyNum)
      {
         System.out.print(a + " ");
      }
      System.out.println();
   }
}


Output:

Given array:
66
67
68
69
70
71

72

Between range 2 and 6:
68 69 70 71


Different variations of copyOfRange()

MethodDescription
static long[] copyOfRange(long[] original, int from, int to)Copies the specified range of the specified array into a new array.
static short[] copyOfRange( short[] original, int from, int to)Copies the specified range of the specified array into a new array.
static T[] copyOfRange(T[] original, int from, int to)Copies the specified range of the specified array into a new array.
static T[] copyOfRange(U[] original, int from, int to, Class newType)Copies the specified range of the specified array into a new array.
static int[] copyOfRange(int[] original, int from, int to)Copies the specified range of the specified array into a new array.
static float[] copyOfRange(float[] original, int from, int to)Copies the specified range of the specified array into a new array.
static double[] copyOfRange( double[] original, int from, int to)Copies the specified range of the specified array into a new array.
static char[] copyOfRange(char[] original, int from, int to)Copies the specified range of the specified array into a new array.
static byte[] copyOfRange(byte[] original, int from, int to)Copies the specified range of the specified array into a new array.
static boolean[] copyOfRange( boolean[] original, int from, int to)Copies the specified range of the specified array into a new array.

Arrays copyOfRange() long data type

Syntax

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

Example:

import java.util.Arrays;
public class ArrayCopyLong
{
   public static void main(String[] args)
   {
      long[] arrLong1 = new long[] {14, 23, 41};
      System.out.println("ARRAY COPY OF RANGE METHOD - LONG DATA TYPE");
      System.out.println("-------------------------------------------");
      System.out.println("Given array : ");
      for(int a = 0; a < arrLong1.length; a++)
      {
         System.out.println(arrLong1[a]);
      }
      // java copyofrange
      long[] arrLong2 = Arrays.copyOfRange(arrLong1, 2, 5);
      System.out.println("Copied array : ");
      for(int a = 0; a < arrLong2.length; a++)
      {
         System.out.println(arrLong2[a]);
      }
   }
}


Output:

Arrays copyOfRange() method in java

Arrays copyOfRange() short data type

Syntax

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

Also read – java array length

Here’s arrays copyofrange short data type example.

import java.util.Arrays;
public class ArrayCopyShort
{
   public static void main(String[] args)
   {
      short[] arrShort1 = new short[] {14, 23, 41};
      System.out.println("ARRAY COPY OF RANGE METHOD - SHORT DATA TYPE");
      System.out.println("--------------------------------------------");
      System.out.println("Given array : ");
      for(int a = 0; a < arrShort1.length; a++)
      {
         System.out.println(arrShort1[a]);
      }
      // java copyofrange
      short[] arrShort2 = Arrays.copyOfRange(arrShort1, 1, 3);
      System.out.println("Copied array : ");
      for(int a = 0; a < arrShort2.length; a++)
      {
         System.out.println(arrShort2[a]);
      }
   }
}


Output:

Arrays copyOfRange() method in java

Also read – java arraylist