Arrays copyOfRange() in java

Let’s learn Arrays copyOfRange() in java.

Arrays copyOfRange() in java

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.

Syntax:

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 arrays copyofrange example,

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  
      // array copy of 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
      // java copyofrange
      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]);
      }

      // 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() 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

Now let’s learn arrays copyofrange 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() in java