Arrays Copyofrange

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn arrays copyofrange.

Arrays copyofrange method

Array class contains many methods for manipulating arrays. It also has many static methods that makes array to be viewed as lists.

Syntax:

java.lang.Object
     java.util.Arrays

public class Arrays extends object

Methods:

These are few methods of different data types

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 array range – long data type

Syntax

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

Parameters

original – from where range is to be copied.

from – is the initial index to be copied (inclusive).

to – is the final index to be copied and this index may lie outside the array (exclusive).

Returns

  • an array is returned containing the specified range from the original array truncated with zeros to get required length.

Exception

  •  NullPointerException
  • ArrayIndexOutOfBoundsException
  • IllegalArgumentException

Note

Inclusive here in array copy of range method of long data type means the initial index of the range (from) should lie between zero and original.length.

Exclusive here in array copy of range method of long data type means the final index of the range (to), which must be greater than or equal to “from”, may be greater than original.length.

Also Read – Array Java

In which case “0L” 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”.

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[] 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


Java array range – short data type

Syntax

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

Parameters

original – from where range is to be copied.

from – is the initial index to be copied (inclusive).

to – is the final index to be copied and this index may lie outside the array (exclusive).

Returns

  • an array is returned containing the specified range from the original array truncated with zeros to get required length.

Exception

  •  NullPointerException
  • ArrayIndexOutOfBoundsException
  • IllegalArgumentException

Note

Inclusive here in array copy of range method of short data type means the initial index of the range (from) should lie between zero and original.length.

Exclusive here in array copy of range method of short data type means the final index of the range (to), which must be greater than or equal to “from”, may be greater than original.length.

Also Read – 2d Array Java

In which case “(short) 0” 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”.

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

      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


Array arrays copy of range – t [ ] type

Syntax

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

Type parameters

T – is the class of the objects in the array.

Parameters

original – from where range is to be copied.

from – is the initial index to be copied (inclusive).

to – is the final index to be copied and this index may lie outside the array (exclusive).

Returns

  • an array is returned containing the specified range from the original array truncated with zeros to get required length.

Exception

  •  NullPointerException
  • ArrayIndexOutOfBoundsException
  • IllegalArgumentException

Note

Inclusive here in array copy of range method of T[ ] type means the initial index of the range (from) should lie between zero and original.length.

Exclusive here in array copy of range method of T[ ] type means the final index of the range (to), which must be greater than or equal to “from”, may be greater than original.length.

Also Read – System Arraycopy

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”.

Arrays copyofrange example

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


conclusion

That’s it guys. This is all about arrays copyofrange method. I hope you have understood the concept of array copy of range method in java.

You can subscribe to my blog flower brackets if you haven’t. If you like this post you share it across social networks.

You May Also Like