64.md 4.7 KB
Newer Older
W
wizardforcel 已提交
1
# Java `Vector`
W
wizardforcel 已提交
2 3 4

> 原文: [https://www.programiz.com/java-programming/vector](https://www.programiz.com/java-programming/vector)

W
wizardforcel 已提交
5
#### 在本教程中,我们将学习`Vector`类以及如何使用它。 我们还将学习它与`ArrayList`类的不同之处,以及为什么我们应该改用`ArrayList`。
W
wizardforcel 已提交
6 7 8 9 10

`Vector`类是`List`接口的实现,它使我们可以创建类似于 [ArrayList](/java-programming/arraylist "Java ArrayList") 类的可调整大小的数组。

* * *

W
wizardforcel 已提交
11
## Java `Vector`与`ArrayList`
W
wizardforcel 已提交
12 13 14

在 Java 中,`ArrayList``Vector`都实现`List`接口并提供相同的功能。 但是,它们之间存在一些差异。

W
wizardforcel 已提交
15
`Vector`类同步每个单独的操作。 这意味着,每当我们要对`Vector`执行某些操作时,`Vector`类都会自动对该操作施加锁定。
W
wizardforcel 已提交
16

W
wizardforcel 已提交
17
这是因为当一个线程正在访问`Vector`时,同时另一个线程试图访问它时,会生成一个称为`ConcurrentModificationException`的异常。 因此,对于每个操作的这种连续使用锁使`Vector`的效率降低。
W
wizardforcel 已提交
18

W
wizardforcel 已提交
19
但是,在`ArrayList`中,方法不同步。 相反,它使用`Collections.synchronizedList()`方法来同步整个列表。
W
wizardforcel 已提交
20

W
wizardforcel 已提交
21
**注意**:建议使用`ArrayList`代替`Vector`,因为`Vector`不是线程安全的,并且效率较低。
W
wizardforcel 已提交
22 23 24

* * *

W
wizardforcel 已提交
25
## 创建`Vector`
W
wizardforcel 已提交
26

W
wizardforcel 已提交
27
这是我们如何用 Java 创建`Vector`
W
wizardforcel 已提交
28 29 30 31 32

```java
Vector<Type> vector = new Vector<>(); 
```

W
wizardforcel 已提交
33
在此,`Type`表示链表的类型。 例如,
W
wizardforcel 已提交
34 35 36 37 38 39 40 41 42 43 44

```java
// create Integer type linked list
Vector<Integer> vector= new Vector<>();

// create String type linked list
Vector<String> vector= new Vector<>(); 
```

* * *

W
wizardforcel 已提交
45
## `Vector`方法
W
wizardforcel 已提交
46 47 48 49 50

`Vector`类还提供`List`接口的可调整大小的数组实现(类似于`ArrayList`类)。 一些`Vector`方法是:

* * *

W
wizardforcel 已提交
51
## 向`Vector`添加元素
W
wizardforcel 已提交
52

W
wizardforcel 已提交
53 54 55
*   `add(element)` - 向`Vector`添加元素
*   `add(index, element)` - 将元素添加到指定位置
*   `addAll(vector)` - 将`Vector`的所有元素添加到另一个`Vector`
W
wizardforcel 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

例如,

```java
import java.util.Vector;

class Main {
    public static void main(String[] args) {
        Vector<String> mammals= new Vector<>();

        // Using the add() method
        mammals.add("Dog");
        mammals.add("Horse");

        // Using index number
        mammals.add(2, "Cat");
        System.out.println("Vector: " + mammals);

        // Using addAll()
        Vector<String> animals = new Vector<>();
        animals.add("Crocodile");

        animals.addAll(mammals);
        System.out.println("New Vector: " + animals);
    }
} 
```

**输出**

```java
Vector: [Dog, Horse, Cat]
New Vector: [Crocodile, Dog, Horse, Cat] 
```

* * *

W
wizardforcel 已提交
93
## 访问`Vector`元素
W
wizardforcel 已提交
94

W
wizardforcel 已提交
95 96
*   `get(index)` - 返回由索引指定的元素
*   `iterator()` - 返回迭代器对象以顺序访问`Vector`元素
W
wizardforcel 已提交
97

W
wizardforcel 已提交
98
例如:
W
wizardforcel 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

```java
import java.util.Iterator;
import java.util.Vector;

class Main {
    public static void main(String[] args) {
        Vector<String> animals= new Vector<>();
        animals.add("Dog");
        animals.add("Horse");
        animals.add("Cat");

        // Using get()
        String element = animals.get(2);
        System.out.println("Element at index 2: " + element);

        // Using iterator()
        Iterator<String> iterate = animals.iterator();
        System.out.print("Vector: ");
        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
        }
    }
} 
```

W
wizardforcel 已提交
126
**输出**
W
wizardforcel 已提交
127 128 129 130 131 132 133 134

```java
Element at index 2: Cat
Vector: Dog, Horse, Cat, 
```

* * *

W
wizardforcel 已提交
135
## 删除`Vector`元素
W
wizardforcel 已提交
136

W
wizardforcel 已提交
137 138 139
*   `remove(index)` - 从指定位置移除元素
*   `removeAll()` - 删除所有元素
*   `clear()` - 删除所有元素。 比`removeAll()`更有效
W
wizardforcel 已提交
140

W
wizardforcel 已提交
141
例如:
W
wizardforcel 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

```java
import java.util.Vector;

class Main {
    public static void main(String[] args) {
        Vector<String> animals= new Vector<>();
        animals.add("Dog");
        animals.add("Horse");
        animals.add("Cat");

        System.out.println("Initial Vector: " + animals);

        // Using remove()
        String element = animals.remove(1);
        System.out.println("Removed Element: " + element);
        System.out.println("New Vector: " + animals);

        // Using clear()
        animals.clear();
        System.out.println("Vector after clear(): " + animals);
    }
} 
```

W
wizardforcel 已提交
167
**输出**
W
wizardforcel 已提交
168 169 170 171 172 173 174 175 176 177

```java
Initial Vector: [Dog, Horse, Cat]
Removed Element: Horse
New Vector: [Dog, Cat]
Vector after clear(): [] 
```

* * *

W
wizardforcel 已提交
178
## 其他`Vector`方法
W
wizardforcel 已提交
179 180

| 方法 | 内容描述 |
W
wizardforcel 已提交
181
| --- | --- |
W
wizardforcel 已提交
182 183 184 185 186
| `set()` | 更改`Vector`的元素 |
| `size()` | 返回`Vector`的大小 |
| `toArray()` | 将`Vector`转换为数组 |
| `toString()` | 将`Vector`转换为字符串 |
| `contains()` | 在`Vector`中搜索指定的元素并返回布尔结果 |