Java Collections Java Java API
Class:
java.util.Arrays
Methods:
These methods copy the specified range of the specified array into a new array.
public static <T> T[] copyOfRange(T[] original,
int from,
int to)
public static <T,U> T[] copyOfRange(U[] original,
int from,
int to,
Class<? extends T[]> newType)
public static byte[] copyOfRange(byte[] original,
int from,
int to)
public static short[] copyOfRange(short[] original,
int from,
int to)
public static int[] copyOfRange(int[] original,
int from,
int to)
public static long[] copyOfRange(long[] original,
int from,
int to)
public static char[] copyOfRange(char[] original,
int from,
int to)
public static float[] copyOfRange(float[] original,
int from,
int to)
public static double[] copyOfRange(double[] original,
int from,
int to)
public static boolean[] copyOfRange(boolean[] original,
int from,
int to)
Examples
package com.logicbig.example.arrays;
import java.util.Arrays;
public class CopyOfRangeExample {
public static void main(String... args) { boolean[] arr = {true, false, true, true, false}; System.out.println(Arrays.toString(arr));
boolean[] arr2 = Arrays.copyOfRange(arr, 2, 5); System.out.println(Arrays.toString(arr2));
} }
Output[true, false, true, true, false] [true, true, false]
package com.logicbig.example.arrays;
import java.util.Arrays;
public class CopyOfRangeExample2 {
public static void main(String... args) { int[] arr = {1, 3, 2, 5, 6, 7, 9};
int[] arr2 = Arrays.copyOfRange(arr, 2, 4); System.out.println(Arrays.toString(arr2)); } }
Output[2, 5]
package com.logicbig.example.arrays;
import java.util.Arrays;
public class CopyOfRangeExample3 {
public static void main(String... args) { int[] arr = {1, 3, 2, 5, 6, 7, 9};
int[] arr2 = Arrays.copyOfRange(arr, 2, 2); System.out.println(Arrays.toString(arr2)); } }
Output[]
package com.logicbig.example.arrays;
import java.util.Arrays;
public class CopyOfRangeExample4 {
public static void main(String... args) { int[] arr = {1, 3, 2, 5, 6, 7, 9};
int[] arr2 = Arrays.copyOfRange(arr, 2, 1); System.out.println(Arrays.toString(arr2)); } }
OutputCaused by: java.lang.IllegalArgumentException: 2 > 1 at java.util.Arrays.copyOfRange(Arrays.java:3591) at com.logicbig.example.arrays.CopyOfRangeExample4.main(CopyOfRangeExample4.java:15) ... 6 more
package com.logicbig.example.arrays;
import java.util.Arrays;
public class CopyOfRangeExample5 {
public static void main(String... args) { Integer[] arr = {1, 3, 2, 5, 6, 7, 9};
Integer[] arr2 = Arrays.copyOfRange(arr, 4, 6); System.out.println(Arrays.toString(arr2)); } }
Output[6, 7]
package com.logicbig.example.arrays;
import java.util.Arrays;
public class CopyOfRangeExample6 {
public static void main(String... args) { Integer[] arr = {1, 3, 2, 5, 6, 7, 9};
Number[] arr2 = Arrays.copyOfRange(arr, 4, 6, Number[].class); System.out.println(Arrays.toString(arr2)); } }
Output[6, 7]
|
|