java.util.Vector.removeElement()方法用於刪除特定對象的首次出現。如果未找到對象,則返回false,否則返回true。
如果向量中存在特定對象,並且對該向量元素調用removeElement()方法,則此方法將向量大小減小1。
用法:
public boolean removeElement(Object obj)
參數:此函數接受對象作為要刪除的參數。
返回類型:刪除成功後,此函數返回True,否則返回False。
異常:此方法不會引發任何異常。
下麵的程序演示了Vector.removeElement()函數。
示例1:
// Java program to understand
// about vector.removeElement() function
// because vector is present in this package
import java.util.*;
// Driver Code
public class vector_demo {
// main method begins here
public static void main(String[] args)
{
// creating vector type object
Vector<Integer> v
= new Vector<Integer>();
// inserting elements into the vector
v.add(1);
v.add(2);
v.add(3);
v.add(4);
v.add(5);
v.add(6);
// printing vector before deleting element
System.out.println("Before deleting");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
System.out.println("\nAfter deleting");
// trying to deleting object 3
boolean flag = v.removeElement(3);
if (flag) {
System.out.println("Element '3' has been removed");
}
else {
System.out.println("Element '3' is not present in Vector");
}
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
}
}
輸出:
Before deleting Vector: [1, 2, 3, 4, 5, 6] Size: 6 After deleting Element '3' has been removed Vector: [1, 2, 4, 5, 6] Size: 5
示例2:
// Java program to understand
// about vector.removeElement() function
// because vector is present in this package
import java.util.*;
// Driver Code
public class vector_demo {
// main method begins here
public static void main(String[] args)
{
// creating vector type object
Vector<Integer> v = new Vector<Integer>();
// inserting elements into the vector
v.add(1);
v.add(2);
v.add(3);
v.add(4);
v.add(5);
v.add(6);
// printing vector before deleting element
System.out.println("Before deleting");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
System.out.println("\nAfter deleting");
// trying to deleting object 15
boolean flag = v.removeElement(15);
// since object 15 is not present flag will be false
if (flag) {
System.out.println("Element '15' has been removed");
}
else {
System.out.println("Element '15' is not present in Vector");
}
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
}
}
輸出:
Before deleting Vector: [1, 2, 3, 4, 5, 6] Size: 6 After deleting Element '15' is not present in Vector Vector: [1, 2, 3, 4, 5, 6] Size: 6
相關用法
- Java Vector contains()用法及代碼示例
- Java Vector add()用法及代碼示例
- Java Vector set()用法及代碼示例
- Java Vector get()用法及代碼示例
- Java Vector removeElementAt()用法及代碼示例
- Java Vector removeIf()用法及代碼示例
- Java Vector removeAll()用法及代碼示例
- Java Vector remove()用法及代碼示例
- Java Vector elements()用法及代碼示例
- Java Vector equals()用法及代碼示例
- Java Vector size()用法及代碼示例
- Java Vector hashCode()用法及代碼示例
- Java Vector firstElement()用法及代碼示例
- Java Vector insertElementAt()用法及代碼示例
- Java Vector lastElement()用法及代碼示例
注:本文由純淨天空篩選整理自ankit15697大神的英文原創作品 Vector removeElement() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。