# 如何在 java 中使用索引删除`Vector`元素

> 原文： [https://beginnersbook.com/2014/06/how-to-remove-vector-elements-using-index-in-java-example/](https://beginnersbook.com/2014/06/how-to-remove-vector-elements-using-index-in-java-example/)

在本教程中，我们将学习如何使用索引从[`Vector`](https://beginnersbook.com/2014/06/search-elements-in-vector-using-index-java-example/)中删除元素。我们将使用`Vector`类的[`remove(int index)`方法](https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html#remove(int))。

`public E remove(int index)`：删除此`Vector`中指定位置的元素。将任何后续元素向左移位（从索引中减去一个）。返回从[`Vector`](https://beginnersbook.com/2013/12/vector-in-java/)中删除的元素。

#### 示例

索引从 0 开始，所以如果我们调用`remove(2)`，它将从`Vector`中删除第 3 个元素。

```java
import java.util.Vector;
public class RemoveExample {
 public static void main(String[] args) {

    // Creating a Vector of Strings
    Vector<String> vector = new Vector<String>();

    //Adding elements to the Vector
    vector.add("C++");
    vector.add("Java");
    vector.add("Cobol");
    vector.add("C");
    vector.add("Oracle");

    System.out.println("Vector elements before remove(): ");
    for(int i=0; i < vector.size(); i++)
    {
       System.out.println(vector.get(i));
    }

    // Removing 3rd element from Vector
    Object obj = vector.remove(2);

    System.out.println("\nElement removed from Vector is:");
    System.out.println(obj);

    System.out.println("\nVector elements after remove():");
    for(int i=0; i < vector.size(); i++)
    {
       System.out.println(vector.get(i));
    }
 }
}
```

**输出：**

```java
Vector elements before remove(): 
C++
Java
Cobol
C
Oracle

Element removed from Vector is:
Cobol

Vector elements after remove():
C++
Java
C
Oracle

```

这是从指定索引中删除`Vector`元素的示例。