Close

Java Collections - Arrays.copyOf() Examples

Java Collections Java Java API 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods copy the specified array, truncating or padding with default values (if necessary) so the copy has the specified length.

public static <T> T[] copyOf(T[] original,
                             int newLength)
public static <T,U> T[] copyOf(U[] original,
                               int newLength,
                               Class<? extends T[]> newType)
public static byte[] copyOf(byte[] original,
                            int newLength)
public static short[] copyOf(short[] original,
                             int newLength)
public static int[] copyOf(int[] original,
                           int newLength)
public static long[] copyOf(long[] original,
                            int newLength)
public static char[] copyOf(char[] original,
                            int newLength)
public static float[] copyOf(float[] original,
                             int newLength)
public static double[] copyOf(double[] original,
                              int newLength)
public static boolean[] copyOf(boolean[] original,
                               int newLength)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;

public class CopyOfExample {

public static void main(String... args) {
boolean[] arr = {false, true, true};
boolean[] newArr = Arrays.copyOf(arr, 5);
System.out.println(Arrays.toString(newArr));
}
}

Output

[false, true, true, false, false]




package com.logicbig.example.arrays;

import java.util.Arrays;

public class CopyOfExample2 {

public static void main(String... args) {
int[] arr = Arrays.copyOf(new int[]{3, 5}, 6);
System.out.println(Arrays.toString(arr));
}
}

Output

[3, 5, 0, 0, 0, 0]




package com.logicbig.example.arrays;

import java.util.Arrays;

public class CopyOfExample3 {

public static void main(String... args) {
char[] arr = Arrays.copyOf(new char[]{'a', 'c'}, 4);
System.out.println(Arrays.toString(arr));
}
}

Output

[a, c,  ,  ]




package com.logicbig.example.arrays;

import java.util.Arrays;

public class CopyOfExample4 {

public static void main(String... args) {
double[] arr = Arrays.copyOf(new double[]{1.3, 2.5}, 4);
System.out.println(Arrays.toString(arr));
}
}

Output

[1.3, 2.5, 0.0, 0.0]

package com.logicbig.example.arrays;

import java.util.Arrays;

public class CopyOfExample5 {

public static void main(String... args) {
String[] arr = Arrays.copyOf(new String[]{"apple", "pie"}, 5);
System.out.println(Arrays.toString(arr));
}
}

Output

[apple, pie, null, null, null]




package com.logicbig.example.arrays;

import java.math.BigInteger;
import java.util.Arrays;

public class CopyOfExample6 {

public static void main(String... args) {
BigInteger[] arr = Arrays.copyOf(
new BigInteger[]{BigInteger.TEN, BigInteger.ONE}, 4
);
System.out.println(Arrays.toString(arr));

}
}

Output

[10, 1, null, null]




package com.logicbig.example.arrays;

import java.util.Arrays;

public class CopyOfExample7 {

public static void main(String... args) {

Number[] arr = Arrays.copyOf(new Integer[]{1, 4}, 5, Number[].class);
System.out.println(Arrays.toString(arr));
}
}

Output

[1, 4, null, null, null]

package com.logicbig.example.arrays;

import java.util.Arrays;

public class CopyOfExample8 {

public static void main(String... args) {
A[] arr = Arrays.copyOf(new B[]{new B(), new B()}, 5, A[].class);
System.out.println(Arrays.toString(arr));
}

private static class A {
}

private static class B extends A{
}

}

Output

[com.logicbig.example.arrays.CopyOfExample8$B@64775c35, com.logicbig.example.arrays.CopyOfExample8$B@22d47061, null, null, null]




See Also