2015-05-19 74 views
0

我很难得到正确的输出,因为我不知道如何布尔正确的工作方法。我有一个ArrayList,我为您在ArrayList中任何重复这里是我的代码如何在一个方法中包含一个布尔值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 

没有重复//输出没有重复时,有明显的重复输出。我怎样才能纠正这一点?

编辑..顺便说一句我只是想,打印是否找到了循环内重复或不

+0

见[防止重复在数组列表条目(http://stackoverflow.com/questions/14192532/how-to-prevent-the-adding-of-duplicate-objects-to-an- arraylist):考虑使用'set' – fantaghirocco

回答

2
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将是真实的,没有其他的迭代会而是在循环结束后继续执行。

1

考虑下面的输入单出认沽:

a,b,c,a,d 

现在白衣代码它会因为d不重复,所以是假的。它会超过一个重复的价值。同样,一旦一个元素喜欢它被重新植入,你不需要经历所有元素的整个循环,因此有一个休息。

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"); 
    } 
} 
+0

你真的认为只是在没有任何解释的情况下发布代码是错误的将有助于理解发生了什么? – GhostCat

+0

嗨。如果我将它插入for循环如果打印后每输出。我想要做的是打印一个单一的输出,如果在整个列表中有重复 – Onedaynerd

+0

@Onedaynerd请检查代码并了解已更改的内容。它不应该改变你每次打印。 – StackFlowed

1

默认情况下,foundfalse。如果发现任何重复,只需设置它true

if(a.equals(b)){ 
     found = true; 
}else { 
     // found = false; 
     // don't do this otherwise it will override previous `found` value 
} 
0
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"); 
     } 


} 
相关问题