GrowingArrayUtils

  • 简介:
    数组的帮助类,包括数组的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;
    }
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,512评论 0赞 16
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 35,248评论 18赞 399
  • Scala与Java的关系 Scala与Java的关系是非常紧密的!! 因为Scala是基于Java虚拟机,也就是...
    灯火gg阅读 3,619评论 1赞 24
  • 十二月 十一月 十月 九月 八月 七月 五月 三月 一月 回首17年,行行走走就这么多地方了,毕竟一颗年轻的心,就...
    暗香晴雪阅读 253评论 0赞 1
  • 话题引入 刚刚阅读了一个简书作者的文章,题目《是自媒体时代,不是你抄我的,就是我抄你的》我也就抄袭说两句,探讨探讨...
    liuzesheng阅读 4,079评论 35赞 36

友情链接更多精彩内容