试图比较对象不能使用IF语句JAVA

时间:2018-09-27 18:09:48

标签: java object if-statement arraylist boolean

我正在编写一种在ArrayList中查找对象的方法。如果可以找到该对象,则将其打印到屏幕上,否则将显示一条错误消息“找不到对象”。我遇到的问题是因为我的方法是对象“ Dodecahedron”,而不是布尔值,所以我无法使用if语句来比较对象是否存在于数组中。我还能怎么处理呢?

这是我主要方法中的代码。

    System.out.print("\tLabel: ");
    label = userInput.nextLine();

    if(myDList.findDodecahedron(label)) {

        System.out.print(myDList.findDodecahedron(label));
    }
    else {
        System.out.print("\t\"" + label + "\" not found");
    }
        System.out.print("\n\nEnter Code [R, P, S, A, D, F, E, or Q]: ");
    break;

这是我的方法。

public Dodecahedron findDodecahedron(String label1In) {
      String label = "";
      String color = "";
      double edge = 0;
      Dodecahedron result = new Dodecahedron(label, color, edge);
      int index = -1;
      for (Dodecahedron d : dList) {
         if (d.getLabel().equalsIgnoreCase(label1In)) { 
            index = dList.indexOf(d);
            break;
         }    
      }
      if (index >= 0) {
         dList.get(index);
         result = dList.get(index);
      }
      return result;
   }

这是我尝试编译时遇到的错误。

DodecahedronListAppMenu.java:99: error: incompatible types: Dodecahedron cannot be converted to boolean
               if(myDList.findDodecahedron(label)) {

4 个答案:

答案 0 :(得分:1)

检查返回值是否为空。

if (myDList.findDodecahedron(label) != null)

如果没有找到任何对象,则需要更新findDodecahedron()以返回null,而不是找到新对象。更改result的初始值可以做到这一点:

Dodecahedron result = null;

或者,如果您发现形状后立即将其返回,则可以摆脱indexresult。无需保存其索引,然后在循环结束后查找索引。

public Dodecahedron findDodecahedron(String label1In) {
   for (Dodecahedron d : dList) {
      if (d.getLabel().equalsIgnoreCase(label1In)) { 
         return d;
      }    
   }
   return null;
}

您还可以使用Java 8流进一步简化它:

public Dodecahedron findDodecahedron(String label1In) {
   return dList.stream()
      .filter(d -> d.getLabel().equalsIgnoreCase(label1In))
      .findAny()
      .orElse(null);
}

答案 1 :(得分:0)

如果您的方法不是返回布尔值而是特定对象,则应将其分配给变量以利用它。 您无需实例化特定对象,就可以像布尔值一样将其返回以对其进行测试。因此,您必须重复方法调用以第二次检索结果,因为您需要将结果打印在std输出中。 这是无奈的,并且代码/处理重复。

这应该看起来像:

Dodecahedron obj = myDList.findDodecahedron(label)
if(obj != null) {
    System.out.print(obj);
}
else {
    System.out.print("\t\"" + label + "\" not found");
}

答案 2 :(得分:0)

您实际上是在这里实现null object design pattern。您可以在findDodecahedron方法中对此进行明确说明(旁注-为了美观起见,我用Java 8样式的流替换了实现,但这并不是必需的):

public static final Dodecahedron NULL_DODECAHEDRON = new Dodecahedron("", "", 0);
public Dodecahedron findDodecahedron(String label1In) {
      return dList.stream()
                  .filter(d -> d.getLabel().equalsIgnoreCase(label1In))
                  .findFirst()
                  .orElse(NULL_DODECAHEDRON);
}

然后在if条件中使用它:

if (!myDList.findDodecahedron(label).equals(NULL_DODECAHEDRON)) {
    System.out.print(myDList.findDodecahedron(label));
} else {
    System.out.print("\t\"" + label + "\" not found");
}

答案 3 :(得分:0)

首先,不要两次调用find方法。将结果分配给本地变量以供重用。

多项选择:

  1. 由于您创建了一个带有空标签的 dummy 对象,如果找不到该对象,则将其返回,因此您可以进行检查。

    Dodecahedron result = myDList.findDodecahedron(label);
    if (result.getLabel().isEmpty()) {
        System.out.print("\t\"" + label + "\" not found");
    } else {
        System.out.print(result);
    }
    

    替代方法是null object design pattern中描述的answer by Mureinik

  2. 更改方法以返回null(未找到)。

    public Dodecahedron findDodecahedron(String label1In) {
        for (Dodecahedron d : dList) {
            if (d.getLabel().equalsIgnoreCase(label1In)) {
                return d;
            }
        }
        return null;
    }
    

    使用:

    Dodecahedron result = myDList.findDodecahedron(label);
    if (result != null) {
        System.out.print(result);
    } else {
        System.out.print("\t\"" + label + "\" not found");
    }
    
  3. 更改方法以返回Optional(Java 8 +)。

    public Optional<Dodecahedron> findDodecahedron(String label1In) {
        for (Dodecahedron d : dList) {
            if (d.getLabel().equalsIgnoreCase(label1In)) {
                return Optional.of(d);
            }
        }
        return Optional.empty();
    }
    

    返回Optional的替代方法是使用流。

    public Optional<Dodecahedron> findDodecahedron(String label1In) {
        return dList.stream()
                    .filter(d -> d.getLabel().equalsIgnoreCase(label1In))
                    .findFirst();
    }
    

    使用:

    Optional<Dodecahedron> result = myDList.findDodecahedron(label);
    if (result.isPresent()) {
        System.out.print(result.get());
    } else {
        System.out.print("\t\"" + label + "\" not found");
    }
    

使用流的选项3是推荐的解决方案。