2026/6/8 12:12:02
网站建设
项目流程
雄安投资建设集团网站,威海做网站公司哪家好,如何卸载电脑是的wordpress,工程信息价查询网站方法表方法功能boolean offer(E e)入队列boolean add(E e)入队列E poll()出队列E remove()出队列E peek()获取队头元素E element()获取队头元素Queue有两组增删查的方法#xff0c;这两组方法实现的效果是一样的#xff0c;那么他们的区别再哪呢#xff1f;我们来查看一下。…方法表方法功能boolean offer(E e)入队列boolean add(E e)入队列E poll()出队列E remove()出队列E peek()获取队头元素E element()获取队头元素Queue有两组增删查的方法这两组方法实现的效果是一样的那么他们的区别再哪呢我们来查看一下。入队列boolean offer(E e)和boolean add(E e)boolean offer(E e)Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add, which can fail to insert an element only by throwing an exception.Params:e – the element to addReturns:true if the element was added to this queue, else falseThrows:ClassCastException – if the class of the specified element prevents it from being added to this queueNullPointerException – if the specified element is null and this queue does not permit null elementsIllegalArgumentException – if some property of this element prevents it from being added to this queue翻译尝试立即将指定元素插入此队列前提是该操作不会违反容量限制。 在使用容量受限的队列时此方法通常优于 add方法因为 add方法在无法插入元素时只能通过抛出异常来表示失败。参数e – 要添加的元素返回如果元素被成功添加到此队列则返回 true否则返回 false抛出ClassCastException – 如果指定元素的类阻止其被添加到此队列NullPointerException – 如果指定元素为 null 且此队列不允许 null 元素IllegalArgumentException – 如果此元素的某些属性阻止其被添加到此队列boolean add(E e)Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.Params:e – the element to addReturns:true (as specified by Collection.add)Throws:IllegalStateException – if the element cannot be added at this time due to capacity restrictionsClassCastException – if the class of the specified element prevents it from being added to this queueNullPointerException – if the specified element is null and this queue does not permit null elementsIllegalArgumentException – if some property of this element prevents it from being added to this queue翻译如果可以在不违反容量限制的情况下立即将指定元素插入此队列则插入该元素成功时返回 true如果当前没有可用空间则抛出 IllegalStateException 异常。参数e - 要添加的元素返回true按照 Collection.add 的规定抛出IllegalStateException - 如果由于容量限制此时无法添加该元素ClassCastException - 如果指定元素的类阻止其被添加到此队列NullPointerException - 如果指定元素为 null 且此队列不允许 null 元素IllegalArgumentException - 如果此元素的某些属性阻止其被添加到此队列区别总结boolean offer(E e)是一种条件性插入操作仅在队列当前未满时成功否则立即静默失败返回 false。与 boolean add(E e)不同boolean offer(E e)在队列已满时不会抛出异常因此更适用于需避免异常处理的流量控制或实时场景。出队列E poll()和E remove()E poll()Retrieves and removes the head of this queue, or returns null if this queue is empty.Returns:the head of this queue, or null if this queue is empty翻译检索并移除此队列的头元素如果此队列为空则返回 null。返回此队列的头元素如果此队列为空则返回 nullE remove()Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.Returns:the head of this queueThrows:NoSuchElementException – if this queue is empty翻译检索并移除此队列的头元素。 此方法与 poll的不同之处在于如果此队列为空它将抛出异常。返回 此队列的头元素抛出 NoSuchElementException - 如果此队列为空区别总结poll()方法与 remove()方法的核心区别在于当队列为空时poll()会安全地返回 null而 remove()会抛出异常poll()更适用于不确定队列是否为空且希望避免异常的场景是一种更稳妥的获取并移除队列头元素的方式。获取队头元素E peek()和E element()E peek()Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.Returns:the head of this queue, or null if this queue is empty翻译检索但不移除此队列的头元素如果此队列为空则返回 null。返回此队列的头元素如果此队列为空则返回 nullE element()Retrieves, but does not remove, the head of this queue. This method differs from peek only in that it throws an exception if this queue is empty.Returns:the head of this queueThrows:NoSuchElementException – if this queue is empty翻译检索但不移除此队列的头元素。此方法与 peek的唯一区别在于如果此队列为空它将抛出异常。返回此队列的头元素抛出NoSuchElementException - 如果此队列为空区别总结当队列为空时peek()会安全地返回 null而 element()会抛出 NoSuchElementException异常。因此在不确定队列是否为空时使用 peek()更安全若确定队列不为空可使用 element()。总结根据对队列空或满时的处理方案可以将Queue的增删查方法分为两组。第一组队列满或空时不抛出异常1.boolean offer(E e)队列满时返回false2.E poll()队列为空时返回null3.E peek()队列为空时返回null第二组队列为满或空时会抛出异常1.boolean add(E e)队列满时抛出IllegalStateException异常2.E remove()队列为空时抛出NoSuchElementException异常3.E element()队列为空时抛出NoSuchElementException异常因此在对队列进行增删查时希望避免异常处理时就调用第一组的方法希望队列为空或满时抛出异常就调用第二组的方法。