Java教程

Java Vector removeElement()

Java Vector类的 removeElement()方法用于删除第一个(索引最低的)事件。此向量的参数。删除元素会使Vector 大小减小一。

语法

以下是 removeElement()方法的声明:
Public boolean removeElement(Object obj)

参数

参数 说明 必需/可选
obj 这是一个将从向量中删除的元素。 必需

返回

如果自变量是向量的组成部分,则 removeElement()方法返回true,否则返回false。

异常

NA

兼容版本

Java 1.2及更高版本

示例1

import java.util.*;
public class VectorRemoveElementExample1 {  
    public static void main(String arg[]) {       
        //Create an empty vector   
        Vector<Integer> vec = new Vector<>(); 
        // Add elements in the vector
        vec.add(1);
        vec.add(2);
        vec.add(3);
        vec.add(4);
        vec.add(5);
        vec.add(6);
        System.out.println("Vector element before removal: " + vec);
        //Remove an element
        vec.removeElement(5);     
        //Checking vector and displays the element
        System.out.println("Vector element after removal: " + vec);
        }            
}
输出:
Vector element before removal: [1, 2, 3, 4, 5, 6]
Vector element after removal: [1, 2, 3, 4, 6]

示例2

import java.util.*;
public class VectorRemoveElementExample2 {  
    public static void main(String arg[]) {       
        //Create an empty Vector
        Vector<String> vec = new Vector<>();               
        //Add elements to the Vector
        vec.add("Rahul");
        vec.add("Karan");
        vec.add("Rahul");
        vec.add("Rohan");                
        //Printing the elements of the Vector
        System.out.println("The elements of a Vector before remove: "+vec);
        //Removing the "Rahul" from the Vector
        boolean b = vec.removeElement("Rahul");
        System.out.println("Is the removal successful?? "+b);                
        //Printing the elements of the Vector
        System.out.println("The elements of the Vector after remove: "+vec);
        }            
}
输出:
The elements of a Vector before remove: [Rahul, Karan, Rahul, Rohan]
Is the removal successful?? true
The elements of the Vector after remove: [Karan, Rahul, Rohan]

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4