-
remove(int index)
java.util.vector.remove(int index)方法用於從特定位置或索引的Vector中刪除元素。
用法:
Vector.remove(int index)
參數:此方法接受整數數據類型的強製參數索引,並指定要從Vector中刪除的元素的位置。
返回值:此方法返回剛剛從向量中刪除的元素。
以下示例程序旨在說明Java.util.Vector.remove(int index)方法:
// Java code to illustrate remove() when position of // element is passed as parameter import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<String> vec_tor = new Vector<String>(); // Use add() method to add elements in the Vector vec_tor.add("Geeks"); vec_tor.add("for"); vec_tor.add("Geeks"); vec_tor.add("10"); vec_tor.add("20"); // Output the Vector System.out.println("Vector: " + vec_tor); // Remove the element using remove() String rem_ele = vec_tor.remove(4); // Print the removed element System.out.println("Removed element: " + rem_ele); // Print the final Vector System.out.println("Final Vector: " + vec_tor); } }
輸出:Vector: [Geeks, for, Geeks, 10, 20] Removed element: 20 Final Vector: [Geeks, for, Geeks, 10]
-
remove(Object o)
java.util.vector.remove(Object o)方法用於從Vector中刪除任何特定元素。
用法:
Vector.remove(Object o)
參數:此方法接受Vector對象類型的強製參數o並指定要從Vector中刪除的元素。
返回值:如果找到指定元素並將其從Vector中刪除,則返回True,否則返回False。
以下示例程序旨在說明Java.util.Vector.remove(Object O)方法:
// Java code to illustrate remove() method import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<String> vec_tor = new Vector<String>(); // Use add() method to add elements in the Vector vec_tor.add("Geeks"); vec_tor.add("for"); vec_tor.add("Geeks"); vec_tor.add("10"); vec_tor.add("20"); // Output the Vector System.out.println("Vector: " + vec_tor); // Remove the head using remove() Boolean rem_ele; rem_ele = vec_tor.remove("Geeks"); // Print the removed element if (rem_ele) System.out.println("Geeks" + " found and removed."); else System.out.println("Geeks" + " not found or removed."); rem_ele = vec_tor.remove("500"); // Print the removed element if (rem_ele) System.out.println("500" + " found and removed."); else System.out.println("500" + " not found or removed."); // Print the final Vector System.out.println("Final Vector: " + vec_tor); } }
輸出:Vector: [Geeks, for, Geeks, 10, 20] Geeks found and removed. 500 not found or removed. Final Vector: [for, Geeks, 10, 20]
相關用法
- Java Vector add()用法及代碼示例
- Java Vector contains()用法及代碼示例
- Java Vector get()用法及代碼示例
- Java Vector set()用法及代碼示例
- Java Vector equals()用法及代碼示例
- Java Vector removeElement()用法及代碼示例
- Java Vector forEach()用法及代碼示例
- Java Vector lastElement()用法及代碼示例
- Java Vector removeAll()用法及代碼示例
- Java Vector insertElementAt()用法及代碼示例
- Java Vector size()用法及代碼示例
- Java Vector firstElement()用法及代碼示例
- Java Vector removeElementAt()用法及代碼示例
- Java Vector removeRange()用法及代碼示例
- Java Vector toString()用法及代碼示例
注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 Vector remove() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。