java Arralist 大小?

发布于 2024-09-06 16:42:00 字数 87 浏览 4 评论 0原文

我们知道,当添加元素时,ArrayList 的大小会增加 50%(如果是 Vector,则为 100%)。我们在哪里可以找到此行为的实现?

谢谢

As we know ArrayList increases its size by 50% when elements are added(100% incase of Vector).Where can we find the implementation for this behavior?

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年慕年 2024-09-13 16:42:00

在ArrayList中:

public void EnsureCapacity(int minCapacity) {
  modCount++;
  int oldCapacity = elementData.length;
  if (minCapacity > oldCapacity) {
    对象 oldData[] = elementData;
    int 新容量 = (旧容量 * 3)/2 + 1;
    if (newCapacity < minCapacity)
      新容量=最小容量;
    // minCapacity 通常接近 size,所以这是一个胜利:
    elementData = Arrays.copyOf(elementData, newCapacity);
  }
}

和矢量:

private void EnsureCapacityHelper(int minCapacity) {
  int oldCapacity = elementData.length;
  if (minCapacity > oldCapacity) {
    对象[] oldData = elementData;
    int newCapacity = (capacityIncrement > 0) ?
        (旧容量 + 容量增量) : (旧容量 * 2);
    if (newCapacity < minCapacity) {
      新容量=最小容量;
    }
    elementData = Arrays.copyOf(elementData, newCapacity);
  }
}

注意: 除非另有设置,capacityIncrement 默认为 0,因此 Vector 的默认行为是每次增加一倍支持数组需要扩展,但如果您设置了capacityIncrement,那么它将随之增加。

此外,在所有情况下(对于 ArrayList 和 Vector),如果新容量仍然不够大,则无论增加的内容是什么,都会被取代,其中如果使用了所需的容量。

In ArrayList:

public void ensureCapacity(int minCapacity) {
  modCount++;
  int oldCapacity = elementData.length;
  if (minCapacity > oldCapacity) {
    Object oldData[] = elementData;
    int newCapacity = (oldCapacity * 3)/2 + 1;
    if (newCapacity < minCapacity)
      newCapacity = minCapacity;
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
  }
}

and Vector:

private void ensureCapacityHelper(int minCapacity) {
  int oldCapacity = elementData.length;
  if (minCapacity > oldCapacity) {
    Object[] oldData = elementData;
    int newCapacity = (capacityIncrement > 0) ?
        (oldCapacity + capacityIncrement) : (oldCapacity * 2);
    if (newCapacity < minCapacity) {
      newCapacity = minCapacity;
    }
    elementData = Arrays.copyOf(elementData, newCapacity);
  }
}

Note: capacityIncrement defaults to 0 unless set otherwise so the default behaviour of a Vector is to double every time the backing array needs to be expanded but if you set capacityIncrement then it will be incremented by that instead.

Also in all cases (for ArrayList and Vector) the increase--regardless of what it is--is superseded if the new capacity still isn't large enough, in which case the required capacity is used.

2024-09-13 16:42:00

我们在哪里可以找到此行为的实现?

在源代码中。您可以在 Sun JDK 安装的“src.zip”文件中找到 Sun Java 类库的源代码。 OpenJDK 6 和 OpenJDK 7 的源代码也可以通过 OpenJDK 项目页面下载。对于其他 Java 实现,请访问 Web 或查阅文档。

注意 - 并非所有 Java 类库都以相同的方式实现这些类。例如,查看 Apache Harmony 项目源代码或 GNU Classpath 项目源代码不会告诉您 Sun JDK 类库是如何工作的。

Where can we find the implementation for this behavior?

In the source code. You can find the source code for the Sun Java class libraries in the "src.zip" file in your Sun JDK installation. The sources for OpenJDK 6 and OpenJDK 7 are also available for download via the OpenJDK Project page. For other Java implementations, look on the web or consult the documentation.

Beware - not all Java class libraries implement these classes the same way. So for example, looking at the Apache Harmony project sources or the GNU Classpath project sources won't tell you how the Sun JDK class libraries work.

能怎样 2024-09-13 16:42:00

..在ArrayList#add的实现中! ?

下面是一个实现:

public boolean add(E object) {
  if (lastIndex == array.length) {
     growAtEnd(1);
  }
  array[lastIndex++] = object;
  modCount++;
  return true;
}

它调用 growAtEnd 并在该方法中我们找到代码片段:

} else {
  int increment = size / 2;
  if (required > increment) {
    increment = required;
  }
  if (increment < 12) {
    increment = 12;
  }
  E[] newArray = newElementArray(size + increment);
  if (size > 0) {
    System.arraycopy(array, firstIndex, newArray, 0, size);
    firstIndex = 0;
    lastIndex = size;
  }
  array = newArray;

.. 其中增量设置为当前列表实际大小的 50%。


docjar 包含 Apache 开源 Java SE 6 平台 Apache Harmony 项目 的源代码。周围有很多不同的 Java 实现,并且只要此方法的接口中没有记录/要求,就不能保证每个实现都显示完全相同的行为(例如以 50% 的步长增加大小)。

.. in the implementation of ArrayList#add !?

Here's an implementation:

public boolean add(E object) {
  if (lastIndex == array.length) {
     growAtEnd(1);
  }
  array[lastIndex++] = object;
  modCount++;
  return true;
}

It calls growAtEnd and inside this method we find the snippet:

} else {
  int increment = size / 2;
  if (required > increment) {
    increment = required;
  }
  if (increment < 12) {
    increment = 12;
  }
  E[] newArray = newElementArray(size + increment);
  if (size > 0) {
    System.arraycopy(array, firstIndex, newArray, 0, size);
    firstIndex = 0;
    lastIndex = size;
  }
  array = newArray;

.. where the increment is set to 50% of the actual size of the current list.


docjar contains the sourcecode of the Apache harmony project, Apaches open source Java SE 6 platform. There a lot of different Java implementations around and it is not guaranteed that each and every implementation shows the exact same behaviour (like increasing size in steps of 50%) as long as it is not documented/required in the interface to this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文