「java中peek」java中peek什么意思

博主:adminadmin 2022-12-22 17:00:06 67

今天给各位分享java中peek的知识,其中也会对java中peek什么意思进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java队列不用写 peek()函数就能调用,为啥栈中要自己写

你说的java自己带的队列类型吗?如:LinkedList,ArrayList……这些都是java自己定义好的,都在它的源代码里,所以可以直接调用。

你这里MyStack类是自己定义的,不写就不能调用。

有一个方法是MyStack extends LinkedList,这样使用继承就可以不写函数直接能调用父类的方法。

推荐你看看Java的Stack类,里面有现成的方法可以用,该类在java.util包中。

java (String) s.peek()是什么意思?

s.peek() 表示的是查看堆栈顶部的对象,但不从堆栈中移除它。

除此之外:

push(E item) 表示的是把项压入堆栈顶部。

pop() 表示的是移除堆栈顶部的对象,并作为此函数的值返回该对象。

empty() 表示的是测试堆栈是否为空。

search(Object o) 表示的是返回对象在堆栈中的位置,以 1 为基数。

以下是从jdk中拿下来的相关方法的源码,可以参看下:

public class StackE extends VectorE {

    /**

     * Creates an empty Stack.

     */

    public Stack() {

    }

    /**

     * Pushes an item onto the top of this stack. This has exactly

     * the same effect as:

     * blockquotepre

     * addElement(item)/pre/blockquote

     *

     * @param   item   the item to be pushed onto this stack.

     * @return  the codeitem/code argument.

     * @see     java.util.Vector#addElement

     */

    public E push(E item) {

addElement(item);

return item;

    }

    /**

     * Removes the object at the top of this stack and returns that

     * object as the value of this function.

     *

     * @return     The object at the top of this stack (the last item

     *             of the ttVector/tt object).

     * @exception  EmptyStackException  if this stack is empty.

     */

    public synchronized E pop() {

E obj;

int len = size();

obj = peek();

removeElementAt(len - 1);

return obj;

    }

    /**

     * Looks at the object at the top of this stack without removing it

     * from the stack.

     *

     * @return     the object at the top of this stack (the last item

     *             of the ttVector/tt object).

     * @exception  EmptyStackException  if this stack is empty.

     */

    public synchronized E peek() {

int len = size();

if (len == 0)

    throw new EmptyStackException();

return elementAt(len - 1);

    }

    /**

     * Tests if this stack is empty.

     *

     * @return  codetrue/code if and only if this stack contains

     *          no items; codefalse/code otherwise.

     */

    public boolean empty() {

return size() == 0;

    }

    /**

     * Returns the 1-based position where an object is on this stack.

     * If the object tto/tt occurs as an item in this stack, this

     * method returns the distance from the top of the stack of the

     * occurrence nearest the top of the stack; the topmost item on the

     * stack is considered to be at distance tt1/tt. The ttequals/tt

     * method is used to compare tto/tt to the

     * items in this stack.

     *

     * @param   o   the desired object.

     * @return  the 1-based position from the top of the stack where

     *          the object is located; the return value code-1/code

     *          indicates that the object is not on the stack.

     */

    public synchronized int search(Object o) {

int i = lastIndexOf(o);

if (i = 0) {

    return size() - i;

}

return -1;

    }

    /** use serialVersionUID from JDK 1.0.2 for interoperability */

    private static final long serialVersionUID = 1224463164541339165L;

}

java中的queue类有哪些用法?

java中的queue类是队列数据结构管理类。在它里边的元素可以按照添加它们的相同顺序被移除。

队列通常(但并非一定)以 FIFO(先进先出)的方式排序各个元素。不过优先级队列和 LIFO 队列(或堆栈)例外,前者根据提供的比较器或元素的自然顺序对元素进行排序,后者按 LIFO(后进先出)的方式对元素进行排序。无论使用哪种排序方式,队列的头都是调用remove()或poll()所移除的元素。在 FIFO 队列中,所有的新元素都插入队列的末尾。其他种类的队列可能使用不同的元素放置规则。每个Queue实现必须指定其顺序属性。

offer 添加一个元素并返回true 如果队列已满,则返回false

poll 移除并返问队列头部的元素 如果队列为空,则返回null

peek 返回队列头部的元素 如果队列为空,则返回null

put 添加一个元素 如果队列满,则阻塞

take 移除并返回队列头部的元素 如果队列为空,则阻塞

element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常

add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常

remove 移除并返回队列头部的元素 如果队列为空,则抛出一个

NoSuchElementException异常

注意:poll和peek方法出错进返回null。因此,向队列中插入null值是不合法的。

还有带超时的offer和poll方法重载,例如,下面的调用:

boolean success = q.offer(x,100,TimeUnit.MILLISECONDS);

尝试在100毫秒内向队列尾部插入一个元素。如果成功,立即返回true;否则,当到达超时进,返回false。同样地,调用:

Object head = q.poll(100, TimeUnit.MILLISECONDS);

如果在100毫秒内成功地移除了队列头元素,则立即返回头元素;否则在到达超时时,返回null。

阻塞操作有put和take。put方法在队列满时阻塞,take方法在队列空时阻塞。

Queue接口与List、Set同一级别,都是继承了Collection接口。LinkedList实现了Queue接 口。Queue接口窄化了对LinkedList的方法的访问权限(即在方法中的参数类型如果是Queue时,就完全只能访问Queue接口所定义的方法 了,而不能直接访问 LinkedList的非Queue的方法),以使得只有恰当的方法才可以使用。BlockingQueue 继承了Queue接口。

关于java中peek和java中peek什么意思的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。