简体   繁体   English

LinkedList 的不兼容类型和错误操作数类型错误

[英]Incompatible Types and Bad Operand Type Error for LinkedList

Hi for my project I'm trying to remove all occurences of the item and return the number of items removed.您好,对于我的项目,我正在尝试删除所有出现的项目并返回删除的项目数。 All subsequent items are moved to the left and if the item is not found, this method will return 0.所有后续项目都移到左侧,如果未找到该项目,则此方法将返回 0。

Here is my code:这是我的代码:

public class LinkedList<T> implements LinkedListInterface<T> {
  private Node head;
  private Node tail;
  private int count;

  public LinkedList() {
    head = null;
    tail = null;
    count = 0;
  }

  class Node {
    T data;
    Node next;

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

  public Node getHead() {
    return head;
  }
public int contains(T item) {
    Node nd = this.head;
    for (int pos = 1; pos <= count; pos++) {
      if (nd.data == item) {
        return pos;
      }
      nd = nd.next;
    }
    return 0;
  }

  public T remove(int pos) throws ListException {
    if (pos < 1 || pos > count) {
        throw new ListException("Invalid position to remove from");
      }
      Node removedItem = null;
      if (count == 1) {
        removedItem = head;
        head = null;
        tail = null;
      }
      else if (pos == 1) {
        removedItem = head;
        head = head.next;
      }
      else if (pos == count) {
        removedItem = tail;
        Node prev = jump(pos - 2);
        prev.next = null;
        tail = prev;
      }
      else {
        Node prev = jump(pos - 2);
        removedItem = prev.next;
        prev.next = prev.next.next;
      }
      count--;
      return removedItem.data;
  }

  public int remove(T item) { 
    Node numRemoved = 0;
    let pos = -1; // cannot find symbol
    while ((pos = contains(item)) > 0) {
      remove(pos);
      numRemoved++; // bad operand type LinkedList<T>.Node for unary operator '++'
    }
    return numRemoved; // LinkedList<T>.Node cannot be converted to int
  }

  public void replace(T item, int pos) throws ListException {
    if (pos < 1 || pos > count + 1) {
      throw new ListException("Invalid position to insert at");
    }
  }

}

Under...在下面...

public int remove(T item) { 
    Node numRemoved = 0;
    let pos = -1; // cannot find symbol
    while ((pos = contains(item)) > 0) {
      remove(pos);
      numRemoved++; // bad operand type LinkedList<T>.Node for unary operator '++'
    }
    return numRemoved; // LinkedList<T>.Node cannot be converted to int
  }

I put a comment line that contains the error and some things I tried to do to fix was instead of我放了一个包含错误的注释行,我试图做的一些事情来修复而不是

let pos = -1 

I put我放

int pos = -1;

but it made my while loop have an error as well.但它使我的 while 循环也有错误。 I think it may have to do with the declaration of the data types for varibles numRemoved and pos.我认为这可能与变量 numRemoved 和 pos 的数据类型声明有关。 There is also a bad operand type for还有一个错误的操作数类型

numRemoved++

Any ideas on how to fix?关于如何修复的任何想法? Thank you so much.太感谢了。

Hello, update, this is what it went down to:您好,更新,这就是它的原因:

public int remove(T item) {
    int numRemoved = 0;
    int pos = -1;
    try {
      while ((pos = contains(item)) > 0) {
        remove(pos);
        numRemoved++;
      }
    } catch (ListException e){
      System.out.print(e);
    }
    return numRemoved;
  }

However, when I tested it out, any matching name(s) in my program couldn't be removed, any reason why?但是,当我对其进行测试时,我的程序中的任何匹配名称都无法删除,有什么原因吗?

There is no such keyword as 'let' in Java. 'int' is the appropriate declaration, as you discovered. Java 中没有“let”这样的关键字。正如您所发现的,“int”是适当的声明。

There's nothing wrong with the while-statement itself, it's one of the statements that happens to be in the loop body. while 语句本身没有任何问题,它是恰好在循环体中的语句之一。 And that is very clear - you cannot apply the '++' operator to a Node.这很清楚——您不能将“++”运算符应用于节点。 That operator is for incrementing integer values.该运算符用于递增 integer 个值。 What do you expect it to do on a Node?你希望它在节点上做什么? Or, equivalently - why have you declared 'numRemoved' to be a Node, when it's obviously supposed to count something?或者,等价地 - 为什么将“numRemoved”声明为一个节点,而它显然应该计数? 'int' is appropriate here. 'int' 在这里是合适的。

Also, you said that fixing the first error "made my while loop have an error as well".另外,您说修复第一个错误“使我的 while 循环也有错误”。 It is fairly common that some errors prevent the compiler from subsequent analysis (the code is already known to be wrong) so that other errors are not seen until you fix the known problems.一些错误会阻止编译器进行后续分析(代码已经知道是错误的),因此在您修复已知问题之前看不到其他错误是很常见的。

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

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