Java Vector remove()
remove() Java Vector类方法用于从向量中删除指定的元素。有两种不同类型的Java remove()方法,可以根据其参数加以区分。这些是:
Java Vector remove(int index)方法
Java Vector remove(Object o)方法
删除(int索引)方法:
此方法删除此向量中指定位置的元素。
remove(Object o)方法:
此方法删除此向量中第一次出现的指定元素。如果向量不包含元素,则它保持不变。
语法
以下是
remove()方法的声明:
public E remove(int index)
public boolean remove(Object obj)
参数
参数 |
说明 |
必需/可选 |
index |
它是将被删除的元素的索引。 |
必需 |
obj |
它是一个元素,如果存在,将从向量中删除。 |
必需 |
返回
remove(int index)方法返回已删除的元素。
如果包含指定的元素remove(Object obj)方法将返回true。
异常
ArrayIndexOutOfBoundsException -如果索引超出范围,即(index <0 || index> = size()),则此方法引发异常。
兼容版本
Java 1.2及更高版本
示例1
import java.util.*;
public class VectorRemoveExample1 {
public static void main(String arg[]) {
//Create an empty Vector
Vector < Integer > in = new Vector < > ();
//Add elements in the vector
in.add(100);
in.add(200);
in.add(300);
in.add(200);
in.add(400);
System.out.println("Values in vector: " +in);
System.out.println("Remove first occourence of element 200: "+in.remove((Integer)200));
System.out.println("Values in vector: " +in);
}
}
输出:
Values in vector: [100, 200, 300, 200, 400]
Remove first occourence of element 200: true
Values in vector: [100, 300, 200, 400]
示例2
import java.util.*;
public class VectorRemoveExample2 {
public static void main(String arg[]) {
//Create an empty Vector
Vector<String> vector = new Vector<>();
//Add elements to the Vector
vector.add("Java");
vector.add("Android");
vector.add("JavaScript");
vector.add("Java");
//Display the elements of the Vector
System.out.println("Elements of the Vector before remove: "+vector); //Removing the Java from the Vector
boolean b = vector.removeElement("Java");
System.out.println("Is the removal successful? "+b);
//Printing the elements of the Vector
System.out.println("Elements of the Vector after remove: "+vector);
}
}
输出:
Elements of the Vector before remove: [Java, Android, JavaScript, Java]
Is the removal successful? true
Elements of the Vector after remove: [Android, JavaScript, Java]
示例3
import java.util.*;
public class VectorRemoveExample3 {
public static void main(String arg[]) {
Vector<Integer> vecObject = new Vector<Integer>(4);
vecObject.add(3);
vecObject.add(5);
vecObject.add(3);
vecObject.add(4);
System.out.println("Values in vector :" +vecObject);
System.out.println("Removed element at index 2 is: " +vecObject.remove(2));
System.out.println("Values in vector after remove: " +vecObject);
}
}
输出:
Values in vector :[3, 5, 3, 4]
Removed element at index 2 is: 3
Values in vector after remove: [3, 5, 4]
示例4
import java.util.*;
public class VectorRemoveExample4 {
public static void main(String arg[]) {
//Create a first empty vector
Vector<String> vec = new Vector<>();
//Add elements in the first vector
vec.add("Facebook");
vec.add("Whatsapp");
vec.add("Twitter");
vec.add("Instagram");
vec.add("Skype");
//Display the elements of a vector
System.out.println("Values in vector :" +vec);
//Remove the element at index -2
System.out.println("Removed element at index -2: " +vec.remove(-2));
//Again Display the elements of a vector
System.out.println("Values in vector after remove: " +vec);
}
}
输出:
Values in vector :[Facebook, Whatsapp, Twitter, Instagram, Skype]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
at java.base/java.util.Vector.elementData(Vector.java:761)
at java.base/java.util.Vector.remove(Vector.java:875)
at myPackage.VectorRemoveExample4.main(VectorRemoveExample4.java:16)