📜  Java中的矢量 removeElement() 方法与示例

📅  最后修改于: 2022-05-13 01:54:27.622000             🧑  作者: Mango

Java中的矢量 removeElement() 方法与示例

Java.util.Vector.removeElement()方法用于删除特定对象的第一次出现。如果找不到对象,则返回 false,否则返回 true。
如果向量中存在特定对象,并且对该向量元素调用 removeElement() 方法,则此方法将向量大小减小 1。

句法:

public boolean removeElement(Object obj)

参数:此函数接受对象作为要删除的参数。

返回类型:删除成功时此函数返回True否则此函数返回False

异常:此方法不会引发任何异常。

下面的程序说明了 Vector.removeElement()函数。

方案一:

// 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 v
            = new Vector();
  
        // 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 v = new Vector();
        // 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