美文网首页
GrowingArrayUtils

GrowingArrayUtils

作者: qpan | 来源:发表于2018-03-27 11:01 被阅读41次
  • 简介:
    数组的帮助类,包括数组的append(附加,尾部插入)和insert(插入)的实现;
    主要就是数组的扩容~~~

  • 扩容
    一般是double,但不保证后续不改

    /**
     * Given the current size of an array, returns an ideal size to which the array should grow.
     * This is typically double the given size, but should not be relied upon to do so in the
     * future.
     */
    public static int growSize(int currentSize) {
        return currentSize <= 4 ? 8 : currentSize * 2;
    }
  • append
    int类型
public static int[] append(int[] array, int currentSize, int element) {
        assert currentSize <= array.length;   //断言

        if (currentSize + 1 > array.length) {
            int[] newArray = new int[growSize(currentSize)];
            System.arraycopy(array, 0, newArray, 0, currentSize);
            array = newArray;
        }
        array[currentSize] = element;
        return array;
    }

泛型实现

public static <T> T[] append(T[] array, int currentSize, T element) {
        assert currentSize <= array.length;

        if (currentSize + 1 > array.length) {
            T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(),
                    growSize(currentSize));
            System.arraycopy(array, 0, newArray, 0, currentSize);
            array = newArray;
        }
        array[currentSize] = element;
        return array;
    }

关键在于:
T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(),
growSize(currentSize));

看下实现

class Array
Creates a new array with the specified component type and length.
public static Object newInstance(Class<?> componentType, int length)
        throws NegativeArraySizeException {
        return newArray(componentType, length);
    }

那 newArray怎么操作的呢?

private static Object newArray(Class<?> componentType, int size) throws NegativeArraySizeException {
         
        if (!componentType.isPrimitive()) {
           //本地方法实现   private static native Object createObjectArray(Class<?> componentType, int length) throws NegativeArraySizeException;
            return createObjectArray(componentType, size);
        } else if (componentType == char.class) {
            return new char[size];
        } else if (componentType == int.class) {
            return new int[size];
        } else if (componentType == byte.class) {
            return new byte[size];
        } else if (componentType == boolean.class) {
            return new boolean[size];
        } else if (componentType == short.class) {
            return new short[size];
        } else if (componentType == long.class) {
            return new long[size];
        } else if (componentType == float.class) {
            return new float[size];
        } else if (componentType == double.class) {
            return new double[size];
        } else if (componentType == void.class) {
            throw new IllegalArgumentException("Can't allocate an array of void");
        }
        throw new AssertionError();
    }

也很好理解,就是这个primitiveType是什么鬼?

   /**
     * Determines if the specified {@code Class} object represents a
     * primitive type.
     *
     * <p> There are nine predefined {@code Class} objects to represent
     * the eight primitive types and void.  These are created by the Java
     * Virtual Machine, and have the same names as the primitive types that
     * they represent, namely {@code boolean}, {@code byte},
     * {@code char}, {@code short}, {@code int},
     * {@code long}, {@code float}, and {@code double}.
     *
     * <p> These objects may only be accessed via the following public static
     * final variables, and are the only {@code Class} objects for which
     * this method returns {@code true}.
     *
     * @return true if and only if this class represents a primitive type
     *
     * @see     java.lang.Boolean#TYPE
     * @see     java.lang.Character#TYPE
     * @see     java.lang.Byte#TYPE
     * @see     java.lang.Short#TYPE
     * @see     java.lang.Integer#TYPE
     * @see     java.lang.Long#TYPE
     * @see     java.lang.Float#TYPE
     * @see     java.lang.Double#TYPE
     * @see     java.lang.Void#TYPE
     * @since JDK1.1
     */
    public boolean isPrimitive() {
      return (primitiveType & 0xFFFF) != 0;
    }

可见: 基本类型(8种)+void 都是primitive类型

另外: array.getClass().getComponentType()又代表什么呢?

   /**
     * Returns the {@code Class} representing the component type of an
     * array.  If this class does not represent an array class this method
     * returns null.
     *
     * @return the {@code Class} representing the component type of this
     * class if this class is an array
     * @see     java.lang.reflect.Array
     * @since JDK1.1
     */
    public Class<?> getComponentType() {
      return componentType;
    }

可见: 这是数组里每个元素的class类型


  • insert
    包括两部分:扩容+插入
 public static long[] insert(long[] array, int currentSize, int index, long element) {
        assert currentSize <= array.length;

        if (currentSize + 1 <= array.length) {
            System.arraycopy(array, index, array, index + 1, currentSize - index);
            array[index] = element;
            return array;
        }

        long[] newArray = new long[growSize(currentSize)];
        System.arraycopy(array, 0, newArray, 0, index);
        newArray[index] = element;
        System.arraycopy(array, index, newArray, index + 1, array.length - index);
        return newArray;
    }

泛型:

    public static <T> T[] insert(T[] array, int currentSize, int index, T element) {
        assert currentSize <= array.length;

        if (currentSize + 1 <= array.length) {
            System.arraycopy(array, index, array, index + 1, currentSize - index);
            array[index] = element;
            return array;
        }

        T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(),
                growSize(currentSize));
        System.arraycopy(array, 0, newArray, 0, index);
        newArray[index] = element;
        System.arraycopy(array, index, newArray, index + 1, array.length - index);
        return newArray;
    }

相关文章

  • GrowingArrayUtils

    简介:数组的帮助类,包括数组的append(附加,尾部插入)和insert(插入)的实现;主要就是数组的扩容~~~...

  • [原创]SparseArray源码解析

    SparseArray源码解析 构造 put GrowingArrayUtils.insert growsize ...

网友评论

      本文标题:GrowingArrayUtils

      本文链接:https://www.haomeiwen.com/subject/fiejcftx.html