get(int index)和elementAt(int index)之间有什么区别?

Joh*_*hen -1 java collections vector

Vector有两种方法可以在一个索引处获取元素.

Vector<Integer> matrix;
matrix = new Vector<Integer>;
matrix.get(0);
matrix.elementAt(0);
Run Code Online (Sandbox Code Playgroud)

看来他们在这里也做同样的事情.

Pet*_*rey 7

不同之处在于像Hashtable和Stack这样的Vector是遗留类,它们在1998年的Java 1.2中被重新设计,将被ArrayList取代.

elementAt(int)是遗留方法

get(int)符合15年前添加的List接口.

简而言之,除非你真的需要,否则不要使用Vector.


Roh*_*ain 7

他们都做同样的工作.您可以访问JavadocVector#elementAt(int),清楚地说明:

此方法的功能与get(int)方法(它是List接口的一部分)相同.

好吧,你不应该再使用Vector新代码了.它是遗留类,长期以来被替换为ArrayList.此外,定义的每个操作Vector都是同步的,这在大多数情况下是不需要的.无论何时你需要,你都应该使用Collections.synchronizedList.

而且你也不能像在代码中那样创建一个参数化类型的数组.所以,你的代码甚至都不会编译.

new Vector<Integer>[100];  // This will not compile. Error: Generic Array Creation
Run Code Online (Sandbox Code Playgroud)