zoukankan      html  css  js  c++  java
  • Lists.newArrayListWithExpectedSize( int estimatedSize)

    Lists.newArrayListWithExpectedSize( int estimatedSize)  构造一个期望长度为estimatedSizeArrayList实例。

    源码:

     

    public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) {
        return new ArrayList(computeArrayListCapacity(estimatedSize));
    }

     

    //计算集合的容量
    @VisibleForTesting
    static int computeArrayListCapacity(int arraySize) {
    //判断是否小于0,小于0则抛出异常。
        CollectPreconditions.checkNonnegative(arraySize, "arraySize");
        return Ints.saturatedCast(5L + (long)arraySize + (long)(arraySize / 10));
    }
    //检查是否小于0
    @CanIgnoreReturnValue
    static int checkNonnegative(int value, String name) {
        if (value < 0) {
            throw new IllegalArgumentException(name + " cannot be negative but was: " + value);
        } else {
            return value;
        }
    }
    //判断计算出来的数组长度是否在int的范围内
    public static int saturatedCast(long value) {
        if (value > 2147483647L) {
            return 2147483647;
        } else {
            return value < -2147483648L ? -2147483648 : (int)value;
        }
    }

     

  • 相关阅读:
    深度优先搜索
    哈希算法
    双指针问题
    基本概念
    Ionic JPush极光推送二
    一条sql获取每个类别最新的一条记录
    Ionic App 更新插件cordova-plugin-app-version
    Ionic跳转到外网地址
    Ionic cordova-plugin-splashscreen
    Web API 上传下载文件
  • 原文地址:https://www.cnblogs.com/fflower/p/10784890.html
Copyright © 2011-2022 走看看