arrays copyofrange java code example
Example 1: Arrays copyOfRange() method in java
import java.util.Arrays;
public class ArrayCopyOfRangeDemo
{
public static void main(String[] args)
{
int[] arrNumber = { 66, 67, 68, 69, 70, 71, 72 };
System.out.println("Given array: ");
for(int a = 0; a < arrNumber.length; a++)
{
System.out.println(arrNumber[a]);
}
int[] copyNum = Arrays.copyOfRange(arrNumber, 2, 6);
System.out.println("----------------------");
System.out.println("Between range 2 and 6: ");
for(int a : copyNum)
{
System.out.print(a + " ");
}
System.out.println();
}
}
Example 2: Arrays copyOfRange() method 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]);
}
}
}