Java的Vector.add()和Vector.addElement()之间的区别?

发布于 2024-09-06 16:28:54 字数 91 浏览 3 评论 0原文

请解释 Vector.add() 方法和 Vector.addElement() 方法之间的区别,并附上示例代码片段

Please explain the difference between the Vector.add() method and the Vector.addElement() method, along with a sample code snippet

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

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

发布评论

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

评论(5

爱,才寂寞 2024-09-13 16:28:54

add() 来自 List 接口,它是 Java 1.2 中添加的 Java Collections Framework 的一部分。 Vector 早于它,并用它进行了改造。具体区别是:

  1. addElement()同步add() 不是。在 Java 集合框架中,如果您希望这些方法同步,请将集合包装在 Collections.synchronizedList() 中;

  2. add() 返回一个布尔值表示成功。 addElement() 具有 void 返回类型。

从技术上来说,同步差异并不是 API 的一部分。这是一个实施细节。

赞成使用 List 方法。就像我说的,如果你想要一个synchronized List,请执行以下操作:

List<String> list = Collections.synchronizedList(new ArrayList<String>());
list.add("hello");

add() comes from the List interface, which is part of the Java Collections Framework added in Java 1.2. Vector predates that and was retrofitted with it. The specific differences are:

  1. addElement() is synchronized. add() isn't. In the Java Collections Framework, if you want these methods to be synchronized wrap the collection in Collections.synchronizedList(); and

  2. add() returns a boolean for success. addElement() has a void return type.

The synchronized difference technically isn't part of the API. It's an implementation detail.

Favour the use of the List methods. Like I said, if you want a synchronized List do:

List<String> list = Collections.synchronizedList(new ArrayList<String>());
list.add("hello");
鸩远一方 2024-09-13 16:28:54

方法签名不同,add 返回 true,而 addElement 为 void。

来自 http://www.docjar.com/html/api/ java/util/Vector.java.html

  153       public synchronized boolean add(E object) {
  154           if (elementCount == elementData.length) {
  155               growByOne();
  156           }
  157           elementData[elementCount++] = object;
  158           modCount++;
  159           return true;
  160       }

223       public synchronized void addElement(E object) {
  224           if (elementCount == elementData.length) {
  225               growByOne();
  226           }
  227           elementData[elementCount++] = object;
  228           modCount++;
  229       }

The method signature is different, add returns true, while addElement is void.

from http://www.docjar.com/html/api/java/util/Vector.java.html

  153       public synchronized boolean add(E object) {
  154           if (elementCount == elementData.length) {
  155               growByOne();
  156           }
  157           elementData[elementCount++] = object;
  158           modCount++;
  159           return true;
  160       }

and

223       public synchronized void addElement(E object) {
  224           if (elementCount == elementData.length) {
  225               growByOne();
  226           }
  227           elementData[elementCount++] = object;
  228           modCount++;
  229       }
柠檬 2024-09-13 16:28:54

javadoc 提到:

公共无效addElement(E obj)

此方法在功能上与 add(E) 方法(属于 List 接口的一部分)相同。

它们同时存在的原因是(来自同一个 javadoc):

从 Java 2 平台 v1.2 开始,此类经过改进以实现 List 接口,使其成为 Java Collections Framework 的成员。

List 有一个 add 方法,因此向 Vector 添加了一个实现,但为了保持向后兼容性,addElement没有被删除

The javadoc mentions that:

public void addElement(E obj)

This method is identical in functionality to the add(E) method (which is part of the List interface).

The reason they both exist is (from the same javadoc):

As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework.

List has an add method, so an implementation was added to Vector, but to maintain backwards-compatibility, addElement wasn't removed

没︽人懂的悲伤 2024-09-13 16:28:54

添加元素

此方法在功能上与 add(Object) 方法(属于 List 接口的一部分)相同。

因此,以下内容之间没有区别:

Vector v = new Vector();
v.addElement( new Object() );

Vector v = new Vector();
v.add( new Object() );

这个类 ( vector ) 自 Java1.0 以来一直存在几乎被 ArrayList 所取代,它的优点是速度稍快。

addElement

This method is identical in functionality to the add(Object) method (which is part of the List interface).

So there is no difference between:

Vector v = new Vector();
v.addElement( new Object() );

and

Vector v = new Vector();
v.add( new Object() );

This class ( vector ) exists since Java1.0 and now is pretty much replaced by ArrayList which has the benefit of being slightly faster.

大姐,你呐 2024-09-13 16:28:54

主要区别-> add() 将始终返回 true,而 addElement() 没有返回值。

部门:
addElement(object) 方法在功能上与 add(Object) 方法(属于 List 接口的一部分)相同。

add(Object ) 是因为 Vector 实现了 List 接口,它是从 Java 1.2 当 Vector 移至 Collections 时出现的:早期版本中的集合类,Vector 和 Hashtable,已被改进以实现集合接口。

addElement 是“原始”Vector 的方法。

在这里找到了这个答案..
Vector 中的 add() 和 addElement() 有什么区别?

main difference -> add() will always return true, while addElement() has no return value.

in dept:
addElement(object) method is identical in functionality to the add(Object) method (which is part of the List interface).

add(Object ) is due the fact that Vector implements List Interface and it is appeared since Java 1.2 when Vector was moved to Collections: The collection classes from earlier releases, Vector and Hashtable, have been retrofitted to implement the collection interfaces.

addElement is "original" Vector's method.

found this answer here..
What is difference between add() and addElement() in Vector?

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