2016-09-29 60 views
-4
ArrayList<History1> hist;  //i have an class History1 
hist= new ArrayList<>(); 

public class History{ 

x=text.gettext; 
y=text1.gettext; 

public History(){ 

for(History hiss: hist){ 

if(hiss.getint()==x && hiss.getint2()==y){ 

System.out.println("Already exists in stack") 

} 
else{ 
hist.add(new History1(x,y)) 

} 
} 

} 

} 

代碼有問題,我無法找到它,這段代碼不知道給定的值是否在arraylist或不....如果它是在arraylist中沒有添加列表中的值其他它將該值添加爲歷史對象。如何知道數值是否在數組列表中?

+1

是x和y整數?因爲你將它們設置爲text.gettext,然後與hiss.getint中的一個進行比較 – Jakob

+2

請分享完整的代碼 –

+1

正如這裏所寫的那樣,存在明顯的問題,使得無法肯定地說。此外,請花時間以一致的方式格式化您的代碼;不要讓我們追逐'{}'s – ChiefTwoPencils

回答

0

這可能是你想要什麼:

if (hist.contains(x) || hist.contains(y)) { 
    System.out.println("Contained in the ArrayList."); 
} else { 
    System.out.println("Not contained in the ArrayList."); 
} 

要小心類型x和y的,他們應該與ArrayList包含的元素的類型。

相關問題