posX 无法解析或不是字段

问题描述 投票:0回答:1

我在处理时使用了一个自行实现的链接列表,并且刚刚发生了错误:

posX 无法解析或不是字段

这是我的代码:

主要课程:


List<Garbage> GarbageList = new List<>();

void removeGarbage(int x,int y){
  if(GarbageList.head.data.posX==x && GarbageList.head.data.posY==y){
    GarbageList.head = GarbageList.head.head;
  } else{
    Node end = GarbageList.head;
      while (end.next != null) {
        if(end.next.data.posX==x && end.next.data.posY==y){
          if(end.next.next =! null){
            end.next = end.next.next;
          } else{
            end.next = null;
          }
        }
        end = end.next;
      }
  }
}


垃圾类别:


class Garbage{
  
  int type;
  int value;
  int posX, posY;
  PImage sprite;

/methods
.
.
.
  
}


节点类别:

class Node<T> {
  T data;
  Node next;

  Node(T data) {
    this.data = data;
    this.next = null;
  }
}

列出类别:


class List<T> {
  Node<T> head;

  void add(T data) {
    Node<T> newNode = new Node<T>(data);
    if (head == null) {
      head = newNode;
    } else {
      Node end = head;
      while (end.next != null) {
        end = end.next;
      }
      end.next = newNode;
    }
  }

}

我试图创建一个函数来根据 X 和 Y 位置删除垃圾,以便将来实现,但它碰巧不起作用

我只是最终淹没了它,因为堆栈溢出要求我添加更多细节,因为我的帖子主要是代码

洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水ODF洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水洪水

java linked-list
1个回答
0
投票
我认为您的代码中有一个小错字。在

removeGarbage

 方法中,您在条件 
if(end.next.next =! null)
 中存在拼写错误。应该是
if(end.next.next != null)
而不是
=!
。此拼写错误导致无法理解 
posX
posY
 作为类中的字段。

if (end.next.next != null) {
此外,您可能需要在访问其属性之前确保 

GarbageList.head

 不为 null,以避免潜在的 NullPointerExceptions

if (GarbageList.head != null && GarbageList.head.data.posX == x && GarbageList.head.data.posY == y) {
    
© www.soinside.com 2019 - 2024. All rights reserved.