建立一个一维的vector:
Vector<Double> v=new Vector<Double>();//实例一个v对象
v.addElement();//在v中添加元素的方法
v.elementAt(i);//取得v中索引为i的元素
c++的 vector使用不需要new
vector<int> v(10);
vector向末尾添加元素有2种方法,add和addElement,有什么区别呢?
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:
-
addElement()
issynchronized
.add()
isn't. In the Java Collections Framework, if you want these methods to be synchronized wrap the collection inCollections.synchronizedList()
; and -
add()
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 a synchronized
List
do:
List<String> list =Collections.synchronizedList(newArrayList<String>());
list.add("hello");
via:http://stackoverflow.com/questions/3089969/difference-between-javas-vector-add-and-vector-addelement
区别基本可以忽略。
vector创建2维数组:
Vector<Vector<Double>> v=
new
Vector<Vector<Double> >();
//Example: get(0,2)
Double d=v.get(
0
).get(
2
);
从jdk5.0开始,新增了泛型功能。泛型可以使把你的定义的对象置于某个容器而不失去其类型。也可以定义泛型类,public class classname <T> {}