我有这段代码:
public static ArrayList type = new ArrayList<Integer>();
///////////some code//////////
System.out.println(normalization.type.get(i));
System.out.println(normalization.type.get(i) == "1");
它给了我输出
1
false
我尝试过单引号和等于方法,仍然不匹配。 有什么想法吗??
最佳答案
Integer 是一个对象,因此您必须使用 equals
方法。
此外,“1”
是一个字符串,因此它不能等于整数1
您必须执行以下操作:
System.out.println(normalization.type.get(i).equals(new Integer(1)));
或者
System.out.println(normalization.type.get(i).equals(1));
关于java - 如何比较数组列表中的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18595012/