Java 的 Vector.add() 和 Vector.addElement() 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3089969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Java's Vector.add() and Vector.addElement()?
提问by JavaUser
Please explain the difference between the Vector.add()method and the Vector.addElement()method, along with a sample code snippet
请解释Vector.add()方法和Vector.addElement()方法之间的区别,并附上示例代码片段
采纳答案by cletus
add()comes from the Listinterface, which is part of the Java Collections Framework added in Java 1.2. Vectorpredates that and was retrofitted with it. The specific differences are:
add()来自List接口,它是 Java 1.2 中添加的 Java Collections Framework 的一部分。Vector早于此并对其进行了改装。具体区别是:
addElement()issynchronized.add()isn't. In the Java Collections Framework, if you want these methods to be synchronized wrap the collection inCollections.synchronizedList(); andadd()returns a boolean for success.addElement()has avoidreturn type.
addElement()是synchronized。add()不是。在 Java Collections Framework 中,如果您希望这些方法同步,请将集合包装在Collections.synchronizedList(); 和add()返回一个布尔值表示成功。addElement()有一个void返回类型。
The synchronizeddifference technically isn't part of the API. It's an implementation detail.
synchronized技术上的差异不是 API 的一部分。这是一个实现细节。
Favour the use of the Listmethods. Like I said, if you want a synchronizedListdo:
赞成使用这些List方法。就像我说的,如果你想做synchronizedList:
List<String> list = Collections.synchronizedList(new ArrayList<String>());
list.add("hello");
回答by Michael Mrozek
The javadocmentions that:
在javadoc中提到:
public void addElement(E obj)
This method is identical in functionality to the add(E) method (which is part of the List interface).
公共无效添加元素(E obj)
此方法在功能上与 add(E) 方法(它是 List 接口的一部分)相同。
The reason they both exist is (from the same javadoc):
它们都存在的原因是(来自同一个 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.
从 Java 2 平台 v1.2 开始,该类经过改造以实现 List 接口,使其成为 Java Collections Framework 的成员。
Listhas an addmethod, so an implementation was added to Vector, but to maintain backwards-compatibility, addElementwasn't removed
List有一个add方法,所以一个实现被添加到Vector,但为了保持向后兼容性,addElement没有被删除
回答by Jubal
The method signature is different, add returns true, while addElement is void.
方法签名不同,add返回true,而addElement为void。
from http://www.docjar.com/html/api/java/util/Vector.java.html
来自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 }
回答by OscarRyz
addElement
添加元素
This method is identical in functionality to the add(Object) method (which is part of the List interface).
此方法在功能上与 add(Object) 方法(它是 List 接口的一部分)相同。
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 ArrayListwhich has the benefit of being slightly faster.
这个类( vector )自 Java1.0 以来就存在,现在几乎被它所取代,ArrayList它的好处是稍微快一点。
回答by solokiran
main difference -> add() will always return true, while addElement() has no return value.
主要区别 -> add() 将始终返回 true,而 addElement() 没有返回值。
in dept: addElement(object) method is identical in functionality to the add(Object) method (which is part of the List interface).
在部门:addElement(object) 方法在功能上与 add(Object) 方法(它是 List 接口的一部分)相同。
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.
add(Object ) 是因为 Vector 实现了 List 接口,它从 Java 1.2 开始出现,当时 Vector 被移动到 Collections:早期版本中的集合类 Vector 和 Hashtable 已经被改造以实现集合接口。
addElement is "original" Vector's method.
addElement 是“原始”Vector 的方法。
found this answer here.. What is difference between add() and addElement() in Vector?
在这里找到了这个答案.. Vector 中的 add() 和 addElement() 有什么区别?

