Java Array Copy Of Range Method

Hey guys!! Welcome to flower brackets. In this post we are going to learn java array copy of range method.

Java Array Copy Of Range 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.

Array Copy Of Range Method – 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 – Best Way To Copy Array In 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:

Java Array Copy Of Range Method


Array Copy Of Range Method – 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 – Array Copy Program In 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:

Java Array Copy Of Range Method


Array Copy Of Range Method – 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.

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

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:

Java Array Copy Of Range Method


Array Copy Of Range Method – u [ ] type

Syntax

public static <T,U> T[ ] copyOfRange( U[ ] original, int from, int to, Class<? extends T[ ] > newType )

Type parameters

U – is the class of objects in original array.

T – is the class of objects in returned 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).

newType – the class of copy to be returned.

Returns

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

Exception

  •  NullPointerException
  • ArrayIndexOutOfBoundsException
  • IllegalArgumentException
  • ArrayStoreException

Note

Inclusive here in array copy of range method of U[] 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 U[] type means the final index of the range (to), which must be greater than or equal to “from”, may be greater than original.length.

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” and the resulting array is of class newType.

Example

import java.util.Arrays;

public class ArrayCopyUT
{
public static void main(String[] args)
{
Short array1[] = new Short[]{14, 23, 41}; System.out.println("ARRAY COPY OF RANGE METHOD - U [] TYPE"); System.out.println("--------------------------------------"); System.out.println("Given array : ");
for (int a = 0; a < array1.length; a++)
{
System.out.println(array1[a]);
}
Number array2[] = Arrays.copyOfRange(array1, 1, 3, Number[].class);
System.out.println("Copied array : ");
for (int a = 0; a < array2.length; a++)
{
System.out.println(array2[a]);
}
}
}

Output:

Java Array Copy Of Range Method


Array Copy Of Range Method – int data type

Syntax

public static int[ ] copyOfRange( int[ ] 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 int 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 int 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.

In which case “zero” 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 ArrayCopyInt
{
public static void main(String[] args)
{
int arrInt1[] = new int[] {14, 23, 41}; System.out.println("ARRAY COPY OF RANGE METHOD - INT DATA TYPE");
System.out.println("------------------------------------------");
System.out.println("Given array : ");
for (int a = 0; a < arrInt1.length; a++)
{
System.out.println(arrInt1[a]);
}
int arrInt2[] = Arrays.copyOfRange(arrInt1, 2, 6); System.out.println("Copied array : ");
for (int i = 0; i < arrInt2.length; i++)
{
System.out.println(arrInt2[i]);
}
}
}

Output:

Java Array Copy Of Range Method


Array Copy Of Range Method – float data type

Syntax

public static float[ ] copyOfRange( float[ ] 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 float 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 float 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.

In which case “0f” 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 ArrayCopyFloat
{
public static void main(String[] args)
{
float[] array1 = new float[] {11f, 22f, 33f}; System.out.println("ARRAY COPY OF RANGE METHOD - FLOAT DATA TYPE");
System.out.println("--------------------------------------------");
System.out.println("Given array : ");
for (int a = 0; a < array1.length; a++)
{
System.out.println(array1[a]);
}
float[] array2 = Arrays.copyOfRange(array1, 1, 5);
array2[2] = 44f;
array2[3] = 55f;
System.out.println("Copied array : ");
for (int a = 0; a < array2.length; a++)
{
System.out.println(array2[a]);
}
}
}

Output:

Java Array Copy Of Range Method


Array Copy Of Range Method – double data type

Syntax

public static double[ ] copyOfRange( double[ ] 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 double 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 double 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.

In which case “0d” 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 ArrayCopyDouble
{
public static void main(String[] args)
{
double[] arrDouble1 = new double[] {14.5, 23.2, 41.5}; System.out.println("ARRAY COPY OF RANGE METHOD - DOUBLE DATA TYPE");
System.out.println("---------------------------------------------");
System.out.println("Given array : ");
for (int a = 0; a < arrDouble1.length; a++)
{
System.out.println(arrDouble1[a]);
}
double[] arrDouble2 = Arrays.copyOfRange(arrDouble1, 1, 4); System.out.println("Copied array : ");
for (int a = 0; a > arrDouble2.length; a++)
{
System.out.println(arrDouble2[a]);
}
}
}

Output:

ARRAY COPY OF RANGE METHOD – DOUBLE DATA TYPE
————————————————————
Given array :
14.5
23.2
41.5
Copied array :
23.2
41.5
0.0


Array Copy Of Range Method – char data type

Syntax

public static char[ ] copyOfRange( char[ ] 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 char 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 char 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.

In which case “\\u000” 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 ArrayCopyChar
{
public static void main(String[] args)
{
char[] arrChar1 = new char[] {'p', 'q', 'r'}; System.out.println("ARRAY COPY OF RANGE METHOD - CHAR DATA TYPE");
System.out.println("-------------------------------------------");
System.out.println("Given array : "); 
for (int a = 0; a < arrChar1.length; a++)
{
System.out.println(arrChar1[a]);
}
char[] arrChar2 = Arrays.copyOfRange(arrChar1, 1, 3); System.out.println("Copied array : ");
for (int a = 0; a < arrChar2.length; a++)
{
System.out.println(arrChar2[a]);
}
}
}

Output:

Java Array Copy Of Range Method


Array Copy Of Range Method – byte data type

Syntax

public static byte[] copyOfRange( byte[] 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 byte 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 byte 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.

In which case “(byte)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 ArrayCopyByte
{
public static void main(String[] args)
{
byte[] arrByte1 = new byte[] {23, 14, 41}; System.out.println("ARRAY COPY OF RANGE METHOD - BYTE DATA TYPE");
System.out.println("-------------------------------------------");
System.out.println("Given array : ");
for (int a = 0; a < arrByte1.length; a++)
{
System.out.println(arrByte1[a]);
}
byte[] arrByte2 = Arrays.copyOfRange(arrByte1, 1, 5); System.out.println("Copied array : ");
for (int a = 0; a < arrByte2.length; a++)
{
System.out.println(arrByte2[a]);
}
}
}

Output:

Java Array Copy Of Range Method


Array Copy Of Range Method – boolean data type

Syntax

public static boolean[] copyOfRange( boolean[] 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 boolean 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 boolean 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.

In which case “false” 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 ArrayCopyBoolean
{
public static void main(String[] args)
{
boolean[] arrBoolean1 = new boolean[] {true, false, false}; System.out.println("ARRAY COPY OF RANGE METHOD - BOOLEAN DATA TYPE");
System.out.println("----------------------------------------------");
System.out.println("Given array : ");
for (int a = 0; a < arrBoolean1.length; a++)
{
System.out.println(arrBoolean1[a]);
}
boolean[] arrBoolean2 = Arrays.copyOfRange(arrBoolean1, 1, 5); System.out.println("Copied array : ");
for (int a = 0; a < arrBoolean2.length; a++)
{
System.out.println(arrBoolean2[a]);
}
}
}

Output:

Java Array Copy Of Range Method


conclusion

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

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

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *