2016-09-29 58 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包含的元素的类型。

相关问题