net.dongliu.commons.lang.collection.Array Maven / Gradle / Ivy
package net.dongliu.commons.lang.collection;
/**
* array utils
*
* @author Dong Liu
*/
public class Array {
/**
* get array
*/
@SafeVarargs
public static T[] of(T... values) {
return values;
}
public static T[] sub(T[] values, int begin) {
return sub(values, begin, values.length);
}
public static T[] sub(T[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
Class> clazz = values.getClass().getComponentType();
T[] newValues = (T[]) java.lang.reflect.Array.newInstance(clazz, end - begin);
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static int[] sub(int[] values, int begin) {
return sub(values, begin, values.length);
}
public static int[] sub(int[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
int[] newValues = new int[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static long[] sub(long[] values, int begin) {
return sub(values, begin, values.length);
}
public static long[] sub(long[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
long[] newValues = new long[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static short[] sub(short[] values, int begin) {
return sub(values, begin, values.length);
}
public static short[] sub(short[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
short[] newValues = new short[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static char[] sub(char[] values, int begin) {
return sub(values, begin, values.length);
}
public static char[] sub(char[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
char[] newValues = new char[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static byte[] sub(byte[] values, int begin) {
return sub(values, begin, values.length);
}
public static byte[] sub(byte[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
byte[] newValues = new byte[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static boolean[] sub(boolean[] values, int begin) {
return sub(values, begin, values.length);
}
public static boolean[] sub(boolean[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
boolean[] newValues = new boolean[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static float[] sub(float[] values, int begin) {
return sub(values, begin, values.length);
}
public static float[] sub(float[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
float[] newValues = new float[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
public static double[] sub(double[] values, int begin) {
return sub(values, begin, values.length);
}
public static double[] sub(double[] values, int begin, int end) {
if (values == null) {
return null;
}
int len = values.length;
if (begin < 0) {
begin += len;
}
if (end < 0) {
end += len;
}
begin = Math.max(begin, 0);
begin = Math.min(begin, len);
end = Math.max(begin, end);
end = Math.min(end, len);
double[] newValues = new double[end - begin];
System.arraycopy(values, begin, newValues, 0, end - begin);
return newValues;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy