Java的Vector.add()和Vector.addElement()之间的区别?
请解释 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
add()
来自List
接口,它是 Java 1.2 中添加的 Java Collections Framework 的一部分。Vector
早于它,并用它进行了改造。具体区别是:addElement()
是同步
。add()
不是。在 Java 集合框架中,如果您希望这些方法同步,请将集合包装在Collections.synchronizedList()
中;add()
返回一个布尔值表示成功。addElement()
具有void
返回类型。从技术上来说,
同步
差异并不是 API 的一部分。这是一个实施细节。赞成使用
List
方法。就像我说的,如果你想要一个synchronized
List
,请执行以下操作:add()
comes from theList
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: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 avoid
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 asynchronized
List
do:方法签名不同,add 返回 true,而 addElement 为 void。
来自 http://www.docjar.com/html/api/ java/util/Vector.java.html
和
The method signature is different, add returns true, while addElement is void.
from http://www.docjar.com/html/api/java/util/Vector.java.html
and
javadoc 提到:
它们同时存在的原因是(来自同一个 javadoc):
List
有一个add
方法,因此向Vector
添加了一个实现,但为了保持向后兼容性,addElement
没有被删除The javadoc mentions that:
The reason they both exist is (from the same javadoc):
List
has anadd
method, so an implementation was added toVector
, but to maintain backwards-compatibility,addElement
wasn't removed添加元素
因此,以下内容之间没有区别:
和
这个类 ( vector ) 自 Java1.0 以来一直存在几乎被 ArrayList 所取代,它的优点是速度稍快。
addElement
So there is no difference between:
and
This class ( vector ) exists since Java1.0 and now is pretty much replaced by
ArrayList
which has the benefit of being slightly faster.主要区别-> 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?