你作为一个Java领域的专家,请完成以下任务:vector
时间: 2023-12-03 15:42:11 浏览: 34
在Java中,可以使用java.util包中的Vector类来实现动态数组。Vector类是一个动态数组类,它可以根据需要自动增长和缩小数组的大小。以下是Vector类的一些常用方法:
1. add(element):将元素添加到Vector的末尾。
2. add(index, element):将元素插入到指定索引处。
3. remove(index):删除指定索引处的元素。
4. get(index):获取指定索引处的元素。
5. set(index, element):将指定索引处的元素替换为新元素。
6. size():返回Vector中元素的数量。
下面是一个使用Vector的示例代码:
```
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("apple");
v.add("banana");
v.add("orange");
System.out.println("Vector elements:" + v);
v.remove(1);
System.out.println("Vector after removing banana:" + v);
System.out.println("Element at index 0:" + v.get(0));
v.set(1, "grape");
System.out.println("Vector after replacing orange with grape:" + v);
System.out.println("Vector size:" + v.size());
}
}
```
输出:
```
Vector elements:[apple, banana, orange]
Vector after removing banana:[apple, orange]
Element at index 0:apple
Vector after replacing orange with grape:[apple, grape]
Vector size:2
```