Java PriorityQueue remove()方法与示例

本文详细介绍了Java中PriorityQueue类的remove()方法,该方法用于从PriorityQueue中删除指定对象的第一个实例。文章通过示例代码展示了如何使用remove()方法,并解释了其返回值和异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PriorityQueue类remove()方法 (PriorityQueue Class remove() method)

  • remove() method is available in java.util package.

    remove()方法在java.util包中可用。

  • remove() method is used to remove one instance of the given object from this PriorityQueue when exists.

    remove()方法用于从该PriorityQueue中删除给定对象的一个​​实例(如果存在)。

  • remove() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    remove()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • remove() method does not throw an exception at the time of removing an object.

    remove()方法在删除对象时不会引发异常。

Syntax:

句法:

    public boolean remove(Object ob);

Parameter(s):

参数:

  • Object ob – represents the element (ele) to be remove from this PriorityQueue.

    对象ob –表示要从此PriorityQueue中删除的元素(ele)。

Return value:

返回值:

The return type of the method is boolean, it returns true when the given element is to be removed successfully otherwise it returns false.

方法的返回类型为boolean ,如果要成功删除给定元素,则返回true,否则返回false。

Example:

例:

// Java program to demonstrate the example 
// of boolean remove(Object ob) method of 
// PriorityQueue

import java.util.*;

public class RemoveOfPriorityQueue {
    public static void main(String args[]) {
        // Instantiate PriorityQueue
        PriorityQueue < String > pq = new PriorityQueue < String > ();

        // By using add() method is add
        // the given element into priority
        // queue
        pq.add("C");
        pq.add("C++");
        pq.add("JAVA");
        pq.add("PHP");
        pq.add("ANDROID");
        pq.add("JAVA");

        // Display PriorityQueue
        System.out.println("PriorityQueue: " + pq);

        // By using remove(object) method is to
        // remove the single instance of given element
        // from this PriorityQueue
        Object ele = pq.remove("JAVA");

        // Display removed element
        System.out.println("pq.remove(JAVA): " + ele);

        // Display PriorityQueue
        System.out.println("PriorityQueue: " + pq);
    }
}

Output

输出量

PriorityQueue: [ANDROID, C, JAVA, PHP, C++, JAVA]
pq.remove(JAVA): true
PriorityQueue: [ANDROID, C, JAVA, PHP, C++]


翻译自: https://www.includehelp.com/java/priorityqueue-remove-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值