Arrays copyOfRange() in java

In this post we are going to learn Arrays copyOfRange() in java.

Arrays.copyOfRange() in java

Java.util.Arrays.copyOfRange() method copies specified range of the original array into a new array. The initial index of the range (from) must lie between 0 and original.length, inclusive.

Also read – treemap in java

Syntax:

java.lang.Object
     java.util.Arrays

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

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.

Let’s see java program,

import java.util.Arrays;

public class CopyOfRangeDemo 
{
   public static void main(String[] args) 
   {
      int arrNumber[] = { 66, 67, 68, 69, 70, 71, 72 };
      // to index is within the range 
      int copyNum[] = Arrays.copyOfRange(arrNumber, 2, 6); 
      for(int a : copyNum)
      {
         System.out.print(a + " ");
      }
      System.out.println(); 
      // assigning 0 to all index out of range 
      int copy[] = Arrays.copyOfRange(arrNumber, 4, arrNumber.length + 3);
      for(int b : copy)
      {
         System.out.print(b + " ");
      }
   }
}

Output:

68 69 70 71
70 71 72 0 0 0


Different methods:

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.

Java program to demonstrate 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]);
      }

      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() in java


Java program to demonstrate short data type

Syntax

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

Also read – multidimensional array in java

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]);
      }

      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() in java


Also read – system arraycopy method in java