我很難得到正確的輸出,因爲我不知道如何布爾正確的工作方法。我有一個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