提交 ed8eaf32 编写于 作者: W wizardforcel

2019-10-10 16:43:58

上级 dd31811c
......@@ -4,7 +4,7 @@
在上一个教程中,我们分享了如何[将`Vector`转换为数组](https://beginnersbook.com/2014/07/how-to-convert-vector-to-string-array-in-java/)。在这里,我们将通过示例看到`Vector``ArrayList`转换。
#### 例
####
在下面的代码片段中,我们有一个字符串的[`Vector`](https://beginnersbook.com/2013/12/vector-in-java/),我们将其转换为字符串的[`ArrayList`](https://beginnersbook.com/2013/12/java-arraylist/) 。我们在下面的示例中遵循的步骤是:
1)创建一个`Vector`并填充它(我们假设`Vector`将具有`String`元素)。
......
......@@ -4,7 +4,7 @@
几个星期前我们分享了[`ArrayList`到`String`数组的转换](https://beginnersbook.com/2013/12/how-to-convert-arraylist-to-string-array-in-java/)的教程。在本教程中,我们将了解如何在 Java 中将`Vector`转换为`String`数组。
#### 例
####
让我们看一下下面的例子,我们将一个字符串`Vector`转换为一个数组。我们使用`Vector`类的[`toString()`方法](https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html#toString())来做到这一点。
`public String toString()`:返回此`Vector`的字符串表示形式,包含每个元素的`String`表示形式。
......
......@@ -4,7 +4,7 @@
在这里,我们将看到如何一次性删除`HashSet`的所有元素。我们可以通过调用`HashSet`类的`clear()`方法来实现。
#### 例
####
```java
import java.util.HashSet;
......
......@@ -100,7 +100,7 @@ HashMap contains: {1=AA, 2=BB, 3=CC, 4=DD}
```
#### 参考文献
#### 参考:
[`HashSet` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html)
[`HashMap` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
\ No newline at end of file
......@@ -75,7 +75,7 @@ public class ArrayListSorting {
现在您必须了解这些接口的重要性。让我们看看如何使用它们以我们的方式完成排序。
## `ArrayList<Object>`的排序与`Comparable`
## 使用`Comparable`排序`ArrayList<Object>`
假设我们需要对`ArrayList<Student>`进行排序。根据学生年龄属性。这是如何做到的 - 首先实现`Comparable`接口,然后覆盖[`compareTo`](https://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html#compareTo(T))方法。
......
......@@ -62,7 +62,7 @@ Key: 9 & Value: GH
Key: 11 & Value: AB
```
#### 参考文献
#### 参考:
* [`Iterator` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html)
* [`HashMap` 文档](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
\ No newline at end of file
......@@ -159,7 +159,7 @@ After Sorting:
4: Z
```
#### 参考文献
#### 参考:
* [`HashMap` Javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
* [`Comparator` 文档](https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html)
......
......@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/07/java-remove-mapping-from-hashmap-example/](https://beginnersbook.com/2014/07/java-remove-mapping-from-hashmap-example/)
#### 例
####
在这个例子中,我们将看到如何使用键值对的键值从[`HashMap`](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/)中删除特定的映射。我们将使用以下`HashMap`类方法来执行此操作:
......
......@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/07/java-remove-all-mappings-from-hashmap-example/](https://beginnersbook.com/2014/07/java-remove-all-mappings-from-hashmap-example/)
#### 例
####
在上一篇教程中,我们分享了如何[根据键](https://beginnersbook.com/2014/07/java-remove-mapping-from-hashmap-example/)`HashMap`中删除特定的映射。在这个例子中,我们将看到如何从`HashMap`中删除所有映射。我们将使用[`HashMap`类](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/)[`clear()`方法](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clear())来做到这一点:
......
......@@ -6,7 +6,7 @@
`public boolean containsKey(Object key)`:如果此映射包含指定键的映射,则返回`true`
#### 例
####
我们在以下示例中遵循的步骤是:
......
......@@ -8,7 +8,7 @@
反序列化:这是一个从文件中读取`Object`及其属性以及`Object`内容的过程。
#### 例:
#### 例:
**`HashMap`的序列化:**在下面的类中,我们将`HashMap`内容存储在`hashmap.ser`序列化文件中。运行以下代码后,它将生成一个`hashmap.ser`文件。此文件将在下一个类中用于反序列化。
......@@ -105,7 +105,7 @@ key: 33 & Value: EF
key: 3 & Value: IJ
```
#### 参考文献
#### 参考:
* [`HashMap`文档](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
* [`Serializable` javadoc](https://docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html)
......
......@@ -4,7 +4,7 @@
[`HashMap`是一个非同步的集合类](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/)。如果我们需要对它执行线程安全操作,那么我们必须明确地同步它。在本教程中,我们将了解如何同步`HashMap`
#### 例:
#### 例:
在这个例子中,我们有一个`HashMap<Integer, String>`它具有整数键和字符串类型值。为了同步它,我们使用[`Collections.synchronizedMap(hashmap)`](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedMap(java.util.Map))。它返回由指定的`HashMap`备份的线程安全映射。
......
......@@ -100,7 +100,7 @@ HashMap contains: {1=AA, 2=BB, 3=CC, 4=DD}
```
#### 参考文献
#### 参考:
[`HashSet` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html)
[`HashMap` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
\ No newline at end of file
......@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/07/java-hashmap-iterator-example/](https://beginnersbook.com/2014/07/java-hashmap-iterator-example/)
#### 例
####
在上一个教程中,我们看到了[不同的 2 种迭代`HashMap`](https://beginnersbook.com/2013/12/how-to-loop-hashmap-in-java/)的方法。在这个例子中,我们将看到如何使用[`Iterator`](https://beginnersbook.com/2014/06/java-iterator-with-examples/)迭代[`HashMap`](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/)并显示键和值对。我们在下面的例子中遵循的步骤如下:
1)创建[`HashMap`](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html) 并用键值对填充它。
......
......@@ -6,7 +6,7 @@
在提供键时从`HashMap`获取值的程序。
#### 例
####
```java
import java.util.HashMap;
......
......@@ -6,7 +6,7 @@
程序从`HashMap`获取键集。
#### 例
####
```java
import java.util.Iterator;
......
......@@ -7,7 +7,7 @@
克隆`HashMap`的程序。我们将使用`HashMap`类的以下方法来执行克隆。
`public Object clone()`:返回此`HashMap`实例的浅表副本:未克隆键和值本身。
#### 例
####
```java
import java.util.HashMap;
......
......@@ -49,7 +49,7 @@ Key is: Key4 & Value is: Item4
Key is: Key5 & Value is: Item5
```
**参考文献:**
**参考:**
[`TreeMap` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html)
[`Iterator` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html)
......
......@@ -4,7 +4,7 @@
在这个例子中,我们将看到如何从`TreeMap`获取子映射。我们正在使用`TreeMap`类的`subMap()`方法。有关详细信息,请参阅以下程序中的注释。
#### 例
####
```java
import java.util.*;
......@@ -61,7 +61,7 @@ SortedMap Contains : {Key2=Rick, Key3=Kate, Key4=Tom}
TreeMap Contains : {Key1=Jack, Key2=Rick, Key3=Kate, Key5=Steve, Key6=Ram}
```
**参考文献:**
**参考:**
[`subMap()`方法 - `TreeMap` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html#subMap(K,%20K))
[SortedMap javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html)
\ No newline at end of file
......@@ -6,7 +6,7 @@
在本教程中,我们将了解如何创建`Hashtable`,如何填充其条目,然后我们将学习如何使用`Enumeration`显示其键值对。在本文的最后,我们将看到`Hashtable`类的`Hashtable`教程和方法。
#### 例
####
```java
import java.util.Hashtable;
......@@ -92,7 +92,7 @@ Key: Key5 & Value: Mona
14)`Object get(Object key)`:返回指定键映射到的值,如果此映射不包含键的映射,则返回`null`
#### 参考文献
#### 参考:
1. [`Hashtable` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html)
2. [`Enumeration` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html)
\ No newline at end of file
......@@ -7,7 +7,7 @@
`public boolean contains(Object o)`
如果列表包含`Object`,则返回`true`,否则返回`false`
#### 例:
#### 例:
在这个例子中,我们有两个`String`类型的`ArrayList``al1``al2`。我们使用`contains()`方法比较了这些`ArrayList`,并将比较结果存储在第三个`ArrayList``al3``al4`)中。
......
......@@ -4,7 +4,7 @@
当我们处理对象的[`ArrayList`](https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/)时,必须覆盖[`toString()`方法](https://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#toString())以获得所需格式的输出。在本教程中,我们将了解如何在 Java 中覆盖`ArrayList``toString()`方法。
#### 例:
#### 例:
我们这里有两个类`Student``Demo``Student`只有两个属性学生姓名和学生年龄。正如您所看到的,我们已经在`Student`类本身中重写了`toString()`方法。在`Demo`类中,我们将学生对象存储在[`ArrayList`](https://beginnersbook.com/2013/12/how-to-initialize-an-arraylist/) 中,然后我们[使用高级`for`循环](https://beginnersbook.com/2013/12/how-to-loop-arraylist-in-java/)迭代`ArrayList`。您可以很好地看到输出采用我们在`toString()`中指定的格式。您可以根据需要提供`toString()`编码。
......
......@@ -4,7 +4,7 @@
在本教程中,我们将了解如何在 Java 中**连接(或组合)两个`ArrayList`。我们将使用[`addAll()`](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll(java.util.Collection))方法在最后一个`ArrayList`中连接两个 ArrayLists。**
#### 例:
#### 例:
在这个例子中,我们将两个`ArrayList`合并在一个`ArrayList`中,然后显示最终`List`的元素。
......
......@@ -54,7 +54,7 @@ synchronized (list) {
}
```
#### 参考文献
#### 参考:
* [`Vector`文档](https://docs.oracle.com/javase/6/docs/api/java/util/Vector.html)
* [`ArrayList`文档](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html)
\ No newline at end of file
......@@ -32,7 +32,7 @@ HashMap<Integer, String> hmap= new HashMap<Integer, String>();
6) **`get`方法**:在`ArrayList`中我们可以**通过指定它的索引得到**元素。在`HashMap`中,通过指定相应的键来获取元素。
#### 参考文献
#### 参考:
* [`HashMap` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
* [`ArrayList`文档](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html)
\ No newline at end of file
......@@ -33,7 +33,7 @@
2)搜索(`get`方法)操作在`Arraylist``O(1)`)中很快但在`LinkedList``O(n)`中)没有,因此如果添加和删除操作较少且搜索操作要求较多,则`ArrayList`将是您最好的选择。
#### 参考文献
#### 参考:
* [`ArrayList`文档](https://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html)
* [`LinkedList` Javadoc](https://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html)
\ No newline at end of file
......@@ -9,7 +9,7 @@
3. 迭代器
4. `while`循环
#### 例:
#### 例:
在这个例子中,我们有一个`String`类型的`LinkedList`,我们使用所有四个提到的方法循环它。
......
......@@ -6,7 +6,7 @@
使用`LinkedList`类的`add(E e)`方法向`LinkedList`添加新元素的程序。
#### 例
####
```java
import java.util.LinkedList;
......
......@@ -7,7 +7,7 @@
有关此方法的更多信息,请参见 [javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#add(int,%20E)) :
`public void add(int index, E element)`:将指定元素插入此列表中的指定位置。将当前位置的元素(如果有)和任何后续元素向右移动(将其添加到其索引中)。
#### 例
####
在这个例子中,我们有一个`LinkedList<String>`,我们使用`add()`方法将一个元素添加到它的第五个位置(第四个索引)。完整的代码如下:
......
......@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/07/java-add-elements-at-beginning-and-end-of-linkedlist-example/](https://beginnersbook.com/2014/07/java-add-elements-at-beginning-and-end-of-linkedlist-example/)
#### 例
####
在这个例子中,我们将学习如何在`LinkedList`的开头和结尾添加元素。我们将使用`LinkedList`类的[`addFirst()`](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#addFirst(E))和[`addLast()`方法](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#addLast(E))。方法定义和描述如下:
......
......@@ -7,7 +7,7 @@
1)[从特定索引中删除元素](https://beginnersbook.com/2014/07/java-remove-element-from-a-specific-index-in-linkedlist-example/)
2)[从`LinkedList`中删除特定元素](https://beginnersbook.com/2014/07/java-remove-specific-elements-from-linkedlist-example/)
#### 例
####
我们使用[`removeFirst()`方法](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#removeFirst())删除第一个和[`removeLast()`方法](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#removeLast())以从`LinkedList`中删除最后一个元素。方法定义和描述如下:
......
......@@ -4,7 +4,7 @@
在这个例子中,我们将看到如何从`LinkedList`中删除一个元素。
#### 例
####
我们将使用`LinkedList`类的[`remove(int index)`方法](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#remove(int))从特定索引中删除元素。方法定义和描述如下:
......
......@@ -4,7 +4,7 @@
在上一篇文章中,我们在[上分享了如何从`LinkedList`](https://beginnersbook.com/2014/07/java-remove-element-from-a-specific-index-in-linkedlist-example/)中的特定索引中删除元素的教程。在这里,我们将学习如何从[`LinkedList`](https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/)中删除特定元素。
#### 例
####
我们将使用[`remove(Object o)`方法](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#remove(java.lang.Object))来执行此删除。有关此方法的更多信息如下:
......
......@@ -6,7 +6,7 @@
`public void clear()`:删除此列表中的所有元素。此调用返回后,列表将为空。
#### 例
####
在这个例子中,我们有一个`String`类型的`LinkedList`,它有 5 个元素。我们调用`clear()`方法并在调用`clear()`方法之前和之后显示`LinkedList`元素。
......
......@@ -6,7 +6,7 @@
使用`LinkedList`类的`addAll()`方法将`List`的所有元素添加到`LinkedList`的程序。
#### 例
####
```java
import java.util.ArrayList;
......
......@@ -4,7 +4,7 @@
在本教程中,我们将看到一个关于如何从`LinkedList`获取第一个和最后一个元素的示例。
#### 例
####
这里我们有一个`String`类型的`LinkedList`,我们使用`LinkedList`类的[`getFirst()`](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#getFirst())和[`getLast()`](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#getLast())方法得到它的第一个和最后一个元素。方法定义和描述如下:
1)`public E getFirst()`:返回此列表中的第一个元素。
......
......@@ -8,7 +8,7 @@
`public int lastIndexOf(Object o)`:返回此列表中指定元素最后一次出现的索引,如果此列表不包含该元素,则返回-1。
#### 例
####
这里我们有一个`String`元素的`LinkedList`,我们正在使用`LinkedList`类的`indexOf()``lastIndexOf()`方法搜索一个字符串。
......
......@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/07/java-get-sub-list-from-linkedlist-example/](https://beginnersbook.com/2014/07/java-get-sub-list-from-linkedlist-example/)
#### 例
####
在这个例子中,我们使用`LinkedList`类的`subList(int startIndex, int endIndex)`方法获取`LinkedList`的子列表。它返回指定索引`startIndex`(包括)和`endIndex`(不包括)之间的`List`。对子列表所做的任何更改都将反映在原始列表中(我们在下面的程序中通过从子列表中删除元素并在删除后显示原始列表来测试它)。
......
......@@ -4,7 +4,7 @@
在上一篇文章中,我们学习了[如何使用`ListIterator`](https://beginnersbook.com/2014/07/java-linkedlist-listiterator-example/)遍历链表。在这里,我们将学习如何[使用](https://beginnersbook.com/2013/12/how-to-loop-linkedlist-in-java/)[`Iterator`](https://beginnersbook.com/2014/06/java-iterator-with-examples/)迭代`LinkedList`
#### 例
####
我们在以下计划中遵循的步骤是:
......
......@@ -4,7 +4,7 @@
在这个例子中,我们将看到[如何使用](https://beginnersbook.com/2013/12/how-to-loop-linkedlist-in-java/)[`ListIterator`](https://beginnersbook.com/2014/06/listiterator-in-java-with-examples/)迭代`LinkedList`。使用`Listterator`,我们可以在两个方向(向前和向后)迭代列表。除了遍历之外,我们还可以在迭代期间修改列表,并获取迭代器在列表中的当前位置。在[`ListIterator` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html) 上阅读更多相关信息。
#### 例
####
这里我们有一个链接的字符串列表,我们使用`ListIterator`在两个方向上遍历它。
......
......@@ -4,7 +4,7 @@
在上一篇文章中,我们分享了[在`LinkedList`中搜索元素](https://beginnersbook.com/2014/07/java-search-elements-in-linkedlist-example/)的教程。在这里,我们将看到如何使用元素的索引用[`LinkedList`](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) 中的新值替换现有元素值。
#### 例
####
我们在下面的程序中使用的方法是:
`public E set(int index, E element)`:用指定的元素替换此列表中指定位置的元素。
......
......@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/08/clone-a-generic-linkedlist-in-java/](https://beginnersbook.com/2014/08/clone-a-generic-linkedlist-in-java/)
#### 例
####
```java
import java.util.LinkedList;
......
......@@ -10,7 +10,7 @@
`public E peekLast()`:检索但不删除此列表的最后一个元素,如果此列表为空,则返回`null`
#### 例
####
```java
import java.util.LinkedList;
......
......@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/07/java-convert-a-linkedlist-to-arraylist/](https://beginnersbook.com/2014/07/java-convert-a-linkedlist-to-arraylist/)
#### 例
####
在这个例子中,我们将[`LinkedList`](https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/)转换为[`ArrayList`](https://beginnersbook.com/2013/12/java-arraylist/)。我们有一个`LinkedList<String>`,我们在其中存储了 5 个人的名字。转换后,我们将显示`ArrayList`的元素,以确保`ArrayList`具有与`LinkedList`相同的元素。完整的计划如下:
......
......@@ -4,7 +4,7 @@
[`LinkedList`](https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/)转换为数组非常简单。您可以将任何类型的`LinkedList`(例如`double``String``int`等)转换为相同类型的数组。在本教程中,我们将看到此类转换的示例。
#### 例
####
这里我们将`LinkedList`的字符串转换为`String`数组(`LinkedList``String[]`)。在程序结束时,我们已经提到了如何将任何其他类型的列表(如`double``int`等)转换为相应的数组类型。
......
......@@ -107,11 +107,11 @@ Apple Orange Mango Fig fruit1 fruit2 fruit3
13. `boolean removeAll(Collection c)`:它从向量中删除所有存在于`Collection c`中的元素。
14. `void setElementAt(Object element, int index)`:它使用给定元素更新指定索引的元素。
## 向量教程
## `Vector`教程
这是 beginnersbook.com 上发布的向量教程列表。学习快乐!
这是 beginnersbook.com 上发布的`Vector`教程列表。学习快乐!
#### 向量基础知识
#### `Vector`基础知识
* [从`Vector`获取子列表](https://beginnersbook.com/2014/06/how-to-get-sub-list-of-vector-example-in-java/)
* [使用`Collections.sort()`对向量进行排序](https://beginnersbook.com/2014/06/how-to-sort-vector-using-collections-sort-in-java-example/)
......
......@@ -8,7 +8,7 @@
此方法消除了对显式范围操作的需要(对于数组通常存在的排序)。任何期望`List`的操作都可以通过操作`subList`视图而不是整个`List`来用作范围操作。例如,以下语句从`List`中删除了一系列元素:
`list.subList(from, to).clear();`
#### 例
####
```java
import java.util.Vector;
......
# 如何使用 Java 中的`Collections.sort`对`Vector`进行排序 - 示例
# 如何使用 Java 中的`Collections.sort`对`Vector`进行排序
> 原文: [https://beginnersbook.com/2014/06/how-to-sort-vector-using-collections-sort-in-java-example/](https://beginnersbook.com/2014/06/how-to-sort-vector-using-collections-sort-in-java-example/)
......
......@@ -9,7 +9,7 @@
3)`public int lastIndexOf(Object o)`:返回`Vector`中最后一次出现的对象`o`的索引。
4)`public int lastIndexOf(Object o, int startIndex)`:它返回此向量中最后一次出现的指定元素的索引,从`startIndex`(包括)向后搜索。
#### 例
####
在这个例子中,我们使用了上面提到的所有四种方法来证明它们的工作原理。
......
......@@ -5,7 +5,7 @@
在这个例子中,我们将看到如何从`Vector`中删除元素。我们将使用[`Vector` API](https://beginnersbook.com/2013/12/vector-in-java/)的s[`remove(Object o)`方法](https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html#remove(java.lang.Object))以删除指定的元素。
`public boolean remove(Object o)`:从`Vector`中删除第一次出现的指定元素如果`Vector`不包含该元素,则不变。
#### 例
####
在这个例子中,我们从`Vector<String>`中删除两个`String`值。步骤如下:
1)创建`Vector`
......
......@@ -6,7 +6,7 @@
`public E remove(int index)`:删除此`Vector`中指定位置的元素。将任何后续元素向左移位(从索引中减去一个)。返回从[`Vector`](https://beginnersbook.com/2013/12/vector-in-java/)中删除的元素。
#### 例
####
索引从 0 开始,所以如果我们调用`remove(2)`,它将从`Vector`中删除第 3 个元素。
......
......@@ -5,7 +5,7 @@
在这个例子中,我们将看到如何从`Vector`中删除所有元素。我们将使用`Vector`类的[`clear()`方法](https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html#clear())来做到这一点。
`public void clear()`:删除此`Vector`中的所有元素。此方法调用后,`Vector`将为空。
#### 例
####
这里我们在调用`clear()`方法之前和之后显示`Vector`的大小。步骤如下:
1)创建一个`Vector`
......
......@@ -6,7 +6,7 @@
`public E set(int index, E element)`:用指定的元素替换此`Vector`中指定位置的元素。
#### 例
####
在这个例子中,我们用新值替换`Vector`的第 2 和第 3 个元素。
......
......@@ -4,7 +4,7 @@
我们可以使用[`Vector`类](https://beginnersbook.com/2013/12/vector-in-java/)`setSize()`方法设置`Vector`的大小。如果新大小大于当前大小,则当前大小索引之后的所有元素都具有空值。如果新大小小于当前大小,则已从`Vector`中删除当前大小索引之后的元素。
#### 例
####
`Vector`最初有 5 个元素。我们将大小设置为 10,因此在`Vector`的末尾插入了 5 个`null`元素。
在程序的第二部分,我们将大小设置为 4(小于当前大小 10),因此最后六个元素从`Vector`中删除(包括`null`元素)。
......
......@@ -8,7 +8,7 @@
3)通过调用`Vector``iterator()`方法获取迭代器。
4)使用迭代器的`hasNext()``next()`方法遍历`Vector`
#### 例
####
```java
import java.util.Vector;
......
......@@ -4,7 +4,7 @@
我们可以使用[`ListIterator`](https://beginnersbook.com/2014/06/listiterator-in-java-with-examples/)在前进和后退方向上遍历[`Vector`](https://beginnersbook.com/2013/12/vector-in-java/)。除此之外,我们还可以使用 `ListIterator` API 的方法执行其他几个操作,例如显示下一个和前一个元素的索引,替换元素值,在迭代期间删除元素等。
#### 例
####
这里我们有一个字符串向量,我们使用`ListIterator`在两个方向上迭代它。
......@@ -57,7 +57,7 @@ Item2
Item1
```
**参考文献:**
**参考:**
[`ListIterator` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html)
[`Vector` Javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html)
\ No newline at end of file
......@@ -6,7 +6,7 @@
1)创建一个`Vector`并填充它
2)通过调用返回`List`对象的`Collections.list(vector.elements())`将其转换为`List`
#### 例
####
为了解释逻辑,我们假设`Vector`具有`String`元素。但是,如果您想要使用其他类型,则只需更改以下代码中的泛型。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册