一、前言
关于dyuproject-util-1.1.6.jar源码包中的com.dyuproject.util.ArrayUtil数组工具类,对数组合并及两两数组的追加、指定id删除数组内容等操作。
二、源码说明
package com.dyuproject.util;@b@@b@import java.lang.reflect.Array;@b@@b@public final class ArrayUtil@b@{@b@ public static <T> T[] append(T[] oldArray, T toAppend)@b@ {@b@ Class component = oldArray.getClass().getComponentType();@b@ Object[] array = (Object[])(Object[])Array.newInstance(component, oldArray.length + 1);@b@ System.arraycopy(oldArray, 0, array, 0, oldArray.length);@b@ array[oldArray.length] = toAppend;@b@ return array;@b@ }@b@@b@ public static <T> T[] append(T[] oldArray, T toAppend1, T[] toAppend2)@b@ {@b@ Class component = oldArray.getClass().getComponentType();@b@ Object[] array = (Object[])(Object[])Array.newInstance(component, oldArray.length + 1 + toAppend2.length);@b@@b@ System.arraycopy(oldArray, 0, array, 0, oldArray.length);@b@ array[oldArray.length] = toAppend1;@b@ System.arraycopy(toAppend2, 0, array, oldArray.length + 1, toAppend2.length);@b@ return array;@b@ }@b@@b@ public static <T> T[] append(T[] oldArray, T[] toAppend)@b@ {@b@ Class component = oldArray.getClass().getComponentType();@b@ Object[] array = (Object[])(Object[])Array.newInstance(component, oldArray.length + toAppend.length);@b@@b@ System.arraycopy(oldArray, 0, array, 0, oldArray.length);@b@ System.arraycopy(toAppend, 0, array, oldArray.length, toAppend.length);@b@ return array;@b@ }@b@@b@ public static <T> T[] append(T[] oldArray, T[] toAppend1, T toAppend2)@b@ {@b@ Class component = oldArray.getClass().getComponentType();@b@ Object[] array = (Object[])(Object[])Array.newInstance(component, oldArray.length + toAppend1.length + 1);@b@@b@ System.arraycopy(oldArray, 0, array, 0, oldArray.length);@b@ System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length);@b@ array[(array.length - 1)] = toAppend2;@b@ return array;@b@ }@b@@b@ public static <T> T[] append(T[] oldArray, T[] toAppend1, T[] toAppend2)@b@ {@b@ Class component = oldArray.getClass().getComponentType();@b@ Object[] array = (Object[])(Object[])Array.newInstance(component, oldArray.length + toAppend1.length + toAppend2.length);@b@@b@ System.arraycopy(oldArray, 0, array, 0, oldArray.length);@b@ System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length);@b@ System.arraycopy(toAppend2, 0, array, oldArray.length + toAppend1.length, toAppend2.length);@b@ return array;@b@ }@b@@b@ public static <T> T[] remove(T[] oldArray, int idx)@b@ {@b@ if ((idx < 0) || (idx >= oldArray.length))@b@ throw new IllegalArgumentException("array index " + idx + " out of bounds");@b@@b@ Class component = oldArray.getClass().getComponentType();@b@ Object[] array = (Object[])(Object[])Array.newInstance(component, oldArray.length - 1);@b@ if (idx == 0) {@b@ System.arraycopy(oldArray, 1, array, 0, array.length);@b@ }@b@ else {@b@ System.arraycopy(oldArray, 0, array, 0, idx);@b@ System.arraycopy(oldArray, idx + 1, array, idx, array.length - idx);@b@ }@b@ return array;@b@ }@b@}