基于泛型的数组操作工具类


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;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容