当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Vector removeElement()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 Vector removeElement() method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。