简体   繁体   English

错误链表 peek() 不为 null 和 size() =0

[英]Error linkedlist peek() not null and size() =0

import java.util.LinkedList;

public LinkedList<String> mylinkedlist;

/*...*/

System.out.println("STRING: "+ mylinkedlist.peek() +" SIZE: "+ mylinkedlist.size());

output输出

STRING: blablabla SIZE: 0字符串:blablabla 大小:0

it is a debugging question : How can peek() return a value and size of the list be 0 ?这是一个调试问题:如何peek()返回值和列表大小为 0 ?

EDIT FEB 25 - Bug appear again编辑 2 月 25 日 - 错误再次出现

The error :错误 :

java.util.NoSuchElementException
    at java.util.LinkedList.removeFirst(LinkedList.java:270)
    at java.util.LinkedList.pop(LinkedList.java:801)
    at GrblCommunicator.streamCommands(GrblCommunicator.java:164)
    at GrblCommunicator.responseMessage(GrblCommunicator.java:234)
    at ResponseMessageHandler.handleResponse(ResponseMessageHandler.java:33)
    at SerialConnectionJserial.serialEvent(SerialConnectionJserial.java:114)
    at com.fazecast.jSerialComm.SerialPort$SerialPortEventListener.waitForSerialEvent(SerialPort.java:1387)
    at com.fazecast.jSerialComm.SerialPort$SerialPortEventListener$1.run(SerialPort.java:1300)
    at java.lang.Thread.run(Thread.java:748)

The code :编码 :

import java.util.LinkedList;

/*...*/

public volatile LinkedList<String> commandBuffer;     


public void streamCommands() {

        if (this.commandBuffer.size() == 0) {
            // NO-OP
            return;
        }

        if (this.sendPaused) {
            // Another NO-OP
            return;
        }


        while (CommUtils.checkRoomInBuffer(this.sentBufferSize, this.commandBuffer.peek())
                && allowMoreCommands()) {

            if(this.commandBuffer.size()==0) {
                System.out.println("ERROR DE COMMAND BUFFER SIZE GrblComm 155: ");
                return;
            }
            else
            {
                 String commandString = this.commandBuffer.pop();

LINE 164 is : this.commandBuffer.pop();第 164 行是:this.commandBuffer.pop();

I do not understand this bug because there is a this.commandBuffer.size() test !!!!!我不明白这个错误,因为有一个 this.commandBuffer.size() 测试!!!!!!

please, explain me !!!请给我解释一下!!!

EDIT FEB 25编辑 2 月 25 日

Im gonna try to replace LinkedList by ConcurrentLinkedDeque我要尝试用 ConcurrentLinkedDeque 替换 LinkedList

EDIT FEB 26编辑 2 月 26 日

Same error :同样的错误:

java.util.NoSuchElementException
    at java.util.concurrent.ConcurrentLinkedDeque.screenNullResult(ConcurrentLinkedDeque.java:810)
    at java.util.concurrent.ConcurrentLinkedDeque.removeFirst(ConcurrentLinkedDeque.java:990)
    at java.util.concurrent.ConcurrentLinkedDeque.pop(ConcurrentLinkedDeque.java:1036)
    at GrblCommunicator.streamCommands(GrblCommunicator.java:167)
    at GrblCommunicator.responseMessage(GrblCommunicator.java:240)
    at ResponseMessageHandler.handleResponse(ResponseMessageHandler.java:33)
    at SerialConnectionJserial.serialEvent(SerialConnectionJserial.java:114)
    at com.fazecast.jSerialComm.SerialPort$SerialPortEventListener.waitForSerialEvent(SerialPort.java:1391)
    at com.fazecast.jSerialComm.SerialPort$SerialPortEventListener$1.run(SerialPort.java:1295)

It is not possible for you to peek an item in a LinkedList AND have the size of it be zero.您无法查看 LinkedList 中的项目并且使其大小为零。 Think about what you're saying - "How can I look at this value and have the list have no values?".想想你在说什么 - “我怎么能看这个值,让列表没有值?”。 The reason for this is that peek() retrieves the first item in the list but does NOT remove it.这样做的原因是peek()检索列表中的第一项但不删除它。 However , you may be looking for pop() instead.但是,您可能正在寻找pop() Using pop() you can retrieve AND remove the first item at the same time.使用pop()您可以同时检索和删除第一项。 Per the documentation here "Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list".根据此处的文档“从此列表表示的堆栈中弹出一个元素。换句话说,删除并返回此列表的第一个元素”。

LinkedList<String> myList = new LinkedList<>();
myList.add("blablabla");
System.out.println("String: " + myList.pop() + ", Size: " + myList.size());

This prints out:这打印出来:

String: blablabla, Size: 0

For now, the only explantation is in this thread目前,唯一的解释是在这个线程中

"A LinkedList is not thread-safe" “一个 LinkedList 不是线程安全的”

Thread safety issue 线程安全问题

so i will use : LinkedBlockingQueue所以我将使用:LinkedBlockingQueue

If you absolutely need to return a value when the size is 0, you have a few options.如果您绝对需要在大小为 0 时返回一个值,您有几个选择。

  1. Create your own LinkedList implementation创建您自己的LinkedList实现
  2. Catch the exception that will be thrown currently, and then return the value that you find appropriate for this scenario捕获当前将抛出的异常,然后返回您认为适合此场景的值
  3. Check the size of the linked list before calling .peek() and return the appropriate value instead of calling .peek() if the size is 0. If it is not 0, go ahead and make the call.在调用.peek()之前检查链表的大小,如果大小为 0,则返回适当的值而不是调用.peek()如果不是 0,则继续调用。

Implementation of peak() method for java.util.LinkedList says that "Retrieves, but does not remove, the head (first element) of this list." java.util.LinkedList 的 peak() 方法的实现表示“检索但不删除此列表的头部(第一个元素)。”

So with java.util.LinkedList it's not possible.所以使用 java.util.LinkedList 是不可能的。

You may have imported LinkedList from some other library other that "java.util.LinkedList"您可能已经从“java.util.LinkedList”以外的其他库中导入了 LinkedList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM