如何在方法中合并布尔值 JAVA

How to incorporate a boolean in a method JAVA

我很难获得正确的输出,因为我不知道布尔值在方法中的确切工作原理。我有一个数组列表,我正在检查数组列表中是否有重复项这是我的代码

public void rentOneInstrument(List<Instrument> instrumentList){
        String a="";
        String b="";
        boolean found = false;
        for (int j = 0; j < instrumentList.size(); j++) {
                a ="";
                a = instrumentList.get(j).getName();

                for (int i = j+1; i < instrumentList.size(); i++) {
                    b = instrumentList.get(i).getName();
                    System.out.println("a" + a + " b" + b);
                    if(a.equals(b)){
                        found = true;
                    }else {
                        found = false;
                    }
                }
        }
        if (found) {
            System.out.println("duplicate");
        }else{
            System.out.println("no duplicate");
        }
    }

这是我的输出

a Cymbals b Drums,..
a Cymbals b Cello,..
a Cymbals b Cymbals,..
a Drums b Cello,..
a Drums b Cymbals,..
a Cello b Cymbals

no duplicate // 当输出中明显存在重复项时,不打印重复项。我该如何纠正?

编辑..顺便说一句,我只想要一个输出来打印它是否在循环中找到重复项

考虑以下输入:

a,b,c,a,d 

现在你的代码是错误的,因为 d 没有重复。它会使重复的 a 的值过高。此外,一旦一个元素喜欢它被重新种植,你就不需要经历所有元素的整个循环,因此有一个中断。

public void rentOneInstrument(List<Instrument> instrumentList){
        String a="";
        String b="";
        boolean found = false;
        for (int j = 0; j < instrumentList.size(); j++) {
                a ="";
                a = instrumentList.get(j).getName();
                if(found) { break; } // Changed line here 
                for (int i = j+1; i < instrumentList.size(); i++) {
                    b = instrumentList.get(i).getName();
                    System.out.println("temp1 " + a + " temp2 " + b);
                    if(a.equals(b)){
                        found = true;
                        break; // Changed line here 
                    }
                }
        }
        if (found) {
            System.out.println("duplicate");
        }else{
            System.out.println("no duplicate");
        }
    }

另一种不间断的方法是

public void rentOneInstrument(List<Instrument> instrumentList){
        String a="";
        String b="";
        boolean found = false;
        for (int j = 0; j < instrumentList.size() && !found; j++) {
                a ="";
                a = instrumentList.get(j).getName();

                for (int i = j+1; i < instrumentList.size() && !found; i++) { // Changed for condition to look at the found variable too.
                    b = instrumentList.get(i).getName();
                    System.out.println("temp1 " + a + " temp2 " + b);
                    if(a.equals(b)){
                        found = true;
                    }
                }
        }
        if (found) {
            System.out.println("duplicate");
        }else{
            System.out.println("no duplicate");
        }
    }

一个更好的方法是使用不包含重复项的集合。

public void rentOneInstrument(List<Instrument> instrumentList){
     Set< Instrument > instrumentSet = new HashSet< Instrument >(instrumentList);
     if(instrumentList.size()== instrumentSet.size()) {
          System.out.println("no duplicate");
     } else {
          System.out.println("duplicate");
     }
}
if(a.equals(b)){
  found = true
}else {
    found = false;
}

这是你的问题。这样,只有循环的最后一次迭代才会存储在 found 中。由于您将其初始化为 false,因此无需在此处再次将其设置为该值。

   for (int i = j+1; i < instrumentList.size(); i++) {
      b = instrumentList.get(i).getName();
      System.out.println("temp1 " + a + " temp2 " + b);
      if(a.equals(b)){
         found = true;
      }
   }

或者,您可以在找到匹配项时使用 break 语句来跳出循环,如下所示:

if(a.equals(b)){
  found = true;
  break;
}else {
    found = false;
}

这样,found 将为真,不会执行其他迭代,而是在循环结束后继续。

found默认为false。如果发现任何重复项,只需将其设置为 true

 if(a.equals(b)){
      found = true;
 }else {
      // found = false;
      // don't do this otherwise it will override previous `found` value
 }
public void rentOneInstrument(List<Instrument> instrumentList){

    boolean found=false;
    List<String> listString=new ArrayList<String>();

    for (Instrument inst: instrumentList){
        if(listString.contains(inst.getName())){

            found=true;
        }
        else{
            listString.add(inst.getName());
        }


    }

  if (found) {
            System.out.println("duplicate");
        }else{
            System.out.println("no duplicate");
        }


}