本文介绍了什么是矢量容量的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vector API定义了4种不同的构造函数:

The Vector API defines 4 different constructors:

Vector()

Vector(Collection<? extends E> c)

Vector(int initialCapacity)

Vector(int initialCapacity, int capacityIncrement)

但它们如何运作以及它们用于什么?我为什么要为矢量定义固定容量?即使我将初始容量设置为100,我也可以向向量添加101.项:

but how do they work and what are they used for? Why should i want to define a fixed capacity for a vector? Even if i set the initial capacity to 100, I can add a 101. item to the vector:

Vector<Object> test = new Vector<Object>(100);
for (int i = 0; i < 100; i++) {
    test.add(new Object());
}

test.add(new Object());
System.out.println(test.size());
System.out.println(test.capacity());

在上面的代码中,第二个sysout(test.capacity())写入200.为什么是这个向量200的容量?初始容量为100,我没有定义容量增量。

In the above code, the second sysout (test.capacity()) writes 200. Why is the capacity in this vector 200? The initial capacity is 100 and i didn't defined a capacity increment.

我真的不知道是否有使用这些构造函数的真实世界示例?

I really wonder if theres any real world example where these constrcutors are used?

还有一个熟悉的问题:
究竟是Vector.get(int position)和Vector.elementAt(int position)之间的区别?我读到在将Vector添加到Collection类之前定义了get方法,因此有必要在以后添加方法elementAt(int position)。真的吗?或者还有其他差异吗?

And a smiliar question:whats exactly the difference between Vector.get(int position) and Vector.elementAt(int position)? I read that the method get was defined before Vector was added to the Collection class and so it was necessary to add the method elementAt(int position) later too. Is that true? Or are there any other differences?

推荐答案

什么是初始容量?



初始容量就是:施工时 Vector 的容量。

What's an "initial" capacity?

The initial capacity is simply that: the capacity of the Vector at construction time.

A Vector 是一个动态可扩展的数据结构,它会根据需要重新分配其后备阵列。因此,没有最终容量,但您可以设置其初始值是什么。

A Vector is a dynamically growable data structure, and it would reallocate its backing array as necessary. Thus, there is no final capacity, but you can set what its initial value is.

这是一段摘录来自:

Here's an excerpt from Vector API:

注意构建后,你也可以使用达到同样的效果。

Note that post-construction, you can also use ensureCapacity to achieve the same effect.



  • When you create a collection object (List, Set, etc), usually they take a parameter known as "initialCapacity". What does this parameter mean and how is it used ?

比方说,你有100个要插入 Vector 的元素。 (或使用适当的构造函数重载来实现相同的效果)会使过程更有效,因为没有多余的未使用容量,数组只需要重新分配一次。

Note that 4 incremental reallocation steps were performed, and when it finally fits all 100 elements, only 60% of the actual capacity is used. On the other hand, an ensureCapacity(100) (or using the appropriate constructor overload to achieve the same effect) prior to insertion would make the process more efficient, since there's no excess unused capacity, and the array would only need to be reallocated once.

请注意渐近,上述两个过程同样最优 O(N)时间和 O(N)空间),但当然后者是一个恒定时间的改进前者在空间和时间。

Do note that asymptotically, the two processes above are equally optimal (O(N) time and O(N) space), but of course the the latter is a constant-time improvement over the former both in space and time.

当然,如果你设置 ensureCapacity(10000000),并且只插入100元素,你只使用.001%的容量 - 多么浪费!因此,如果您提前知道要插入多少元素,则可以通过使用 ensureCapacity 来提高处理效率(通过常数因子),但在任何case Vector 即使没有你的帮助,它仍然可以自己做得很好。

Of course, if you set ensureCapacity(10000000), and only insert 100 elements, you'd be using only .001% of the capacity -- what a waste! So if you know ahead of time how many elements you're about to insert, you can make the process more efficient (by a constant factor) by using ensureCapacity, but in any case Vector still does a fine job on its own even without your help.



  • Java Set Initial Capacity Best Practice

没有详细说明,这种增长倍增是几何扩展的一种形式,这就是为 Vector 。值得注意的是,,一个由数组支持的类似可扩展数据结构,甚至没有指定其增长策略的详细信息,但的增长因子 3/2

Without going into details, this doubling in growth is a form of geometric expansion, which is what enabled constant-time amortized analysis per operation for Vector. It's worth noting that ArrayList, a similar growable data structure backed by an array, doesn't even specify the details of its growth policy, but the OpenJDK version has a growth factor of 3/2.

请注意 Vector 实际上允许您设置使用。 重要的是要认识到,如果将 capacityIncrement 设置为一个小的非零值,您实际上可以 Vector 执行可怕的渐近。例如,如果将其设置为 1 ,则添加 N 元素将为 O (N ^ 2)操作!

Do note that Vector actually allows you to set a non-geometric growth factor with capacityIncrement. It's important to realize that if you set capacityIncrement to a small non-zero value, you can in fact make Vector perform horrible asymptotically. If you set it to 1, for example, then adding N elements would be an O(N^2) operation!

ArrayList 不允许您自定义增长政策,因为你甚至不应该知道(也不关心,真的!)。

ArrayList doesn't let you customize its growth policy, since you're not even supposed to know (nor care, really!).





    • 请参阅:几何扩展和摊销成本

    • Wikipedia: Dynamic array
      • See: Geometric expansion and amortized cost

      Straight来自


    • ArrayList vs. Vectors in Java if thread safety isn’t a concern

    这篇关于什么是矢量容量的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:48