net.dongliu.commons.collection.ArrayUtils Maven / Gradle / Ivy
The newest version!
package net.dongliu.commons.collection;
import java.lang.reflect.Array;
import java.util.Collection;
/**
* Utils for Array
*
* @author Liu Dong
*/
public final class ArrayUtils {
/**
* create new byte array
*/
public static byte[] byteArray(byte... values) {
return values;
}
/**
* create new char array
*/
public static char[] charArray(char... values) {
return values;
}
/**
* create new short array
*/
public static short[] shortArray(short... values) {
return values;
}
/**
* create new int array
*/
public static int[] intArray(int... values) {
return values;
}
/**
* create new long array
*/
public static long[] longArray(long... values) {
return values;
}
/**
* create new float array
*/
public static float[] floatArray(float... values) {
return values;
}
/**
* create new double array
*/
public static double[] doubleArray(double... values) {
return values;
}
/**
* create new array
*/
@SafeVarargs
public static T[] array(T... values) {
return values;
}
/**
* convert collections to array
*/
public static T[] toArray(Collection c, Class cls) {
@SuppressWarnings("unchecked")
T[] array = (T[]) Array.newInstance(cls, c.size());
int i = 0;
for (T value : c) {
array[i++] = value;
}
return array;
}
}