Arrays copyOfRange in java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn 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:

java.lang.Object
     java.util.Arrays

public class Arrays extends object

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.

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 copyOfRange method – long data type

Syntax

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

Also read – array java

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[] arrLong12 = Arrays.copyOfRange(arrLong1, 2, 5);   
      System.out.println("Copied array : ");
      for (int a = 0; a < arrLong12.length; a++)
      {
         System.out.println(arrLong12[a]);
      }
   }
}

Output:

arrays copyOfRange in java


Java program to demonstrate copyOfRange method – short data type

Syntax

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

Also read – 2d array 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


Java program to demonstrate copyOfRange method – t [ ] type

Syntax

public static <T> T[ ] copyOfRange( T[ ] original, int from, int to )

Also read – system arraycopy

import java.util.Arrays;

public class ArrayCopyT
{
   public static void main(String[] args)
   {
      short array1[] = new short[]{14, 23, 41};   
      System.out.println("ARRAY COPY OF RANGE METHOD - T [] TYPE");
      System.out.println("---------------------------------------");
      System.out.println("Given array : ");
      for(int a = 0; a < array1.length; a++)
      {
         System.out.println(array1[a]);
      }

      Object array2 = Arrays.copyOfRange(array1, 1, 3);
      short array3[] = ( short[] ) array2;
      System.out.println("Copied array : ");
      for(int a = 0; a < array3.length; a++)
      {
         System.out.println(array3[a]);
      }
   }
}

Output:

arrays copyOfRange in java

Related Posts