我很难得到正确的输出,因为我不知道如何布尔正确的工作方法。我有一个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
没有重复//输出没有重复时,有明显的重复输出。我怎样才能纠正这一点?
编辑..顺便说一句我只是想,打印是否找到了循环内重复或不
见[防止重复在数组列表条目(http://stackoverflow.com/questions/14192532/how-to-prevent-the-adding-of-duplicate-objects-to-an- arraylist):考虑使用'set' – fantaghirocco