public class ArraysUtil {
// ---------------------------------------------------------------- wrap
/**
* Wraps elements into an array.
*/
public static <T> T[] array(T... elements) {
return elements;
}
// ---------------------------------------------------------------- join
/**
* Joins arrays. Component type is resolved from the array argument.
*/
@SuppressWarnings({"unchecked"})
public static <T> T[] join(T[]... arrays) {
Class<T> componentType = (Class<T>) arrays.getClass().getComponentType().getComponentType();
return join(componentType, arrays);
}
/**
* Joins arrays using provided component type.
*/
@SuppressWarnings({"unchecked"})
public static <T> T[] join(Class<T> componentType, T[][] arrays) {
if (arrays.length == 1) {
return arrays[0];
}
int length = 0;
for (T[] array : arrays) {
length += array.length;
}
T[] result = (T[]) Array.newInstance(componentType, length);
length = 0;
for (T[] array : arrays) {
System.arraycopy(array, 0, result, length, array.length);
length += array.length;
}
return result;
}
// ---------------------------------------------------------------- resize
/**
* Resizes an array.
*/
public static <T> T[] resize(T[] buffer, int newSize) {
Class<T> componentType = (Class<T>) buffer.getClass().getComponentType();
T[] temp = (T[]) Array.newInstance(componentType, newSize);
System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
return temp;
}
// ---------------------------------------------------------------- append
/**
* Appends an element to array.
*/
public static <T> T[] append(T[] buffer, T newElement) {
T[] t = resize(buffer, buffer.length + 1);
t[buffer.length] = newElement;
return t;
}
// ---------------------------------------------------------------- remove
/**
* Removes sub-array.
*/
public static <T> T[] remove(T[] buffer, int offset, int length) {
Class<T> componentType = (Class<T>) buffer.getClass().getComponentType();
return remove(buffer, offset, length, componentType);
}
/**
* Removes sub-array.
*/
@SuppressWarnings({"unchecked"})
public static <T> T[] remove(T[] buffer, int offset, int length, Class<T> componentType) {
int len2 = buffer.length - length;
T[] temp = (T[]) Array.newInstance(componentType, len2);
System.arraycopy(buffer, 0, temp, 0, offset);
System.arraycopy(buffer, offset + length, temp, offset, len2 - offset);
return temp;
}
// ---------------------------------------------------------------- subarray
/**
* Returns subarray.
*/
public static <T> T[] subarray(T[] buffer, int offset, int length) {
Class<T> componentType = (Class<T>) buffer.getClass().getComponentType();
return subarray(buffer, offset, length, componentType);
}
/**
* Returns subarray.
*/
@SuppressWarnings({"unchecked"})
public static <T> T[] subarray(T[] buffer, int offset, int length, Class<T> componentType) {
T[] temp = (T[]) Array.newInstance(componentType, length);
System.arraycopy(buffer, offset, temp, 0, length);
return temp;
}
// ---------------------------------------------------------------- insert
/**
* Inserts one array into another array.
*/
public static <T> T[] insert(T[] dest, T[] src, int offset) {
Class<T> componentType = (Class<T>) dest.getClass().getComponentType();
return insert(dest, src, offset, componentType);
}
/**
* Inserts one element into an array.
*/
public static <T> T[] insert(T[] dest, T src, int offset) {
Class<T> componentType = (Class<T>) dest.getClass().getComponentType();
return insert(dest, src, offset, componentType);
}
/**
* Inserts one array into another array.
*/
@SuppressWarnings({"unchecked"})
public static <T> T[] insert(T[] dest, T[] src, int offset, Class componentType) {
T[] temp = (T[]) Array.newInstance(componentType, dest.length + src.length);
System.arraycopy(dest, 0, temp, 0, offset);
System.arraycopy(src, 0, temp, offset, src.length);
System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
return temp;
}
/**
* Inserts one element into another array.
*/
@SuppressWarnings({"unchecked"})
public static <T> T[] insert(T[] dest, T src, int offset, Class componentType) {
T[] temp = (T[]) Array.newInstance(componentType, dest.length + 1);
System.arraycopy(dest, 0, temp, 0, offset);
temp[offset] = src;
System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset);
return temp;
}
/**
* Finds the first occurrence in an array from specified given position and upto given length.
*/
public static int indexOf(char[] array, char[] sub, int startIndex, int endIndex) {
int sublen = sub.length;
if (sublen == 0) {
return startIndex;
}
int total = endIndex - sublen + 1;
char c = sub[0];
mainloop:
for (int i = startIndex; i < total; i++) {
if (array[i] != c) {
continue;
}
int j = 1;
int k = i + 1;
while (j < sublen) {
if (sub[j] != array[k]) {
continue mainloop;
}
j++; k++;
}
return i;
}
return -1;
}
}
基于泛型的数组操作工具类
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 学完复杂的泛型,来学点简单的。本文介绍 Java 数组的相关知识。 1. 基本概念 数组是一个容器对象,它包含固定...
- 本人一直都是很喜欢链式编程的风格的,因为简单、易用,还好看。所以闲来无事就整合了一下ImageUtil 图片操作工...