Java Vector removeElementAt() 方法

2025年3月24日 | 阅读时间:2 分钟

Java Vector 类的 removeElementAt() 方法用于删除指定索引处的组件。此向量中大于或等于给定索引的每个元素将向下移动。现在向量的索引比它们之前的值小一。

语法

以下是 removeElementAt() 方法的声明

参数

数据类型参数描述必需/可选
int索引这是将要删除的对象的索引。必需

返回

此方法不返回任何内容。它仅从向量中删除元素。

异常

IndexOutOfBoundsException - 如果数组的索引超出范围(即 (index < 0 || index >= size())),则此方法将抛出此异常。

兼容版本

Java 1.2 及以上版本

示例 1

输出

Vector element before removal: [1, 2, 3, 4, 5, 6]
Vector element after removal: [1, 2, 3, 4, 5]

示例 2

输出

The elements of a vector before removal: [Rahul, Karan, Rahul, Rohan]
The elements of a vector after removal: [Rahul, Karan, Rohan]

示例 3

输出

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50 >= 3
	at java.base/java.util.Vector.removeElementAt(Vector.java:579)
	at myPackage.VectorRemoveElementAtExample3.main(VectorRemoveElementAtExample3.java:12)
 
下一主题Java Vector