【问题标题】:java ~ Unable to qualify condition in an if statementjava ~ 无法在 if 语句中限定条件
【发布时间】:2018-01-26 13:02:35
【问题描述】:

我将 Singly_Linked_List 与泛型一起使用并存储类循环对象,并且循环只有一个参数价格,因此 getElement() 将返回价格。但是 list.contains(walk.getElement()) 总是返回 false。我在 toString 方法中遇到了麻烦。下面给出整个实现。

public String toString() {    
    ArrayList<E> list = new ArrayList<>();
    Node<E> walk = head;
    while (walk != null) {
        if (!list.contains(walk.getElement()))
            list.add(walk.getElement());
        walk = walk.getNext();
    }
    return list.toString();
}


整个列表实现:

 import java.util.ArrayList;
 public class Singly_Linked_List<E> {
  class Node<E> {
    private E element;
    private Node<E> next;

    Node(E element, Node<E> next) {
        this.element = element;
        this.next = next;
    }

    void setNext(Node<E> next) {
        this.next = next;
    }

    E getElement() {
        return element;
    }

    Node<E> getNext() {
        return next;
    }
 }

private Node<E> tail;
private Node<E> head;
private int size = 0;

Singly_Linked_List() {
}

int getSize() {
    return size;
}

boolean isEmpty() {
    return getSize() == 0;
}

void addFirst(E element) {
    head = new Node<E>(element, head);
    if (isEmpty())
        tail = head;
    size++;
}

void addLast(E element) {
    Node<E> node = new Node<E>(element, null);
    if (isEmpty())
        head = node;
    else
        tail.setNext(node);
    tail = node;
    size++;
}

E first() {
    if (isEmpty())
        return null;
    return head.getElement();
}

E last() {
    if (isEmpty())
        return null;
    return tail.getElement();
}

E removeFirst() {
    if (isEmpty()) return null;
    E answer = head.getElement();
    head = head.getNext();
    if (head == tail)
        tail = null;
    size--;
    return answer;
}

@Override
public String toString() {
    ArrayList<E> list = new ArrayList<>();
    Node<E> walk = head;
    while (walk != null) {
        if (!list.contains(walk.getElement()))
            list.add(walk.getElement());
        walk = walk.getNext();
    }
    return list.toString();
}

}
循环类实现:

public class Cycle{
 private int price;


 Cycle(int price) {
     this.price = price;
 }

 int getPrice() {
     return this.price;
 }

 @Override
 public String toString() {
     return "" + this.price;
 }

 public boolean equals(Cycle cycle) {
     return this.getPrice() == cycle.getPrice();
 }
}

【问题讨论】:

  • 请显示对象并列出实现。
  • list 为空。为什么它会包含一些东西?
  • getElement() 返回什么类型?可能是自定义类没有正确实现equals()
  • @jsheeran,第一次是空的,之后就不会空了
  • @IanMc,顺序无关紧要,它应该是唯一的。如果我要删除或添加唯一元素,list.size() 将根据驱动程序方法发生变化。

标签: java generics singly-linked-list


【解决方案1】:

您在 toString 方法中做的第一件事是将一个局部变量列表初始化为一个新的 ArrayList。这意味着 list 在每个 toString 调用开始时为空。这就是为什么 list.contains(...) 总是返回 false 的原因。

如果您的目标是打印出独特的元素,您最好使用 Set 而不是 List。比如:

public String toString() {
    Set<E> s = new HashSet<>();
    Node<E> walk = head;

    while (walk != null) {
      // No need to check if element exists. Set can only contain 
      // unique elements
      s.add(walk.getElement());
      walk = walk.getNext();
    }
    return s.toString();
}

不要忘记为 Node 类实现好的 equals/ hashcode 方法。唯一性就是基于此。

【讨论】:

  • 因为我需要调用toString()方法,在不同的时间和Singly_Linked_List每次都会改变,所以我在方法里面创建了ArrayList。但我认为 Set 会是更好的选择\
【解决方案2】:

您在列表(并为此设置)中出现意外行为(重复)的原因是您的 equals() 方法不正确。 Java 要求您将 Object 传递给 equals。以下是两个应该可以解决您的问题的修复程序

  1. 简单的修复 - 对代码进行最小的更改以使其正常工作(更好的是下面的 #2)

    @Override
    public boolean equals(Object o) {
        Cycle cycle = (Cycle)o;
        return this.getPrice() == cycle.getPrice();
    }
    
  2. 首选的equals方法

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Cycle other = (Cycle) obj;
        if (price != other.price)
            return false;
        return true;
    }
    

您的 IDE 可能能够为您创建 equals()。在创建用于集合、映射等的 equals() 时,最好创建 hashCode()。

【讨论】:

    猜你喜欢
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多