Lists.newArrayListWithExpectedSize( int estimatedSize)

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

源码:

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;
}
}
上一篇:【BZOJ-3306】树 线段树 + DFS序


下一篇:Python+selenium之获取文本值和下拉框选择数据