【发布时间】:2011-02-07 16:46:00
【问题描述】:
我正在存储表示我要跟踪的对象索引的整数对象。稍后在我的代码中,我想检查特定对象的索引是否对应于我之前存储的那些整数之一。我通过创建一个 ArrayList 并从 for 循环的索引创建一个新的 Integer 来做到这一点:
ArrayList<Integer> courseselectItems = new ArrayList();
//Find the course elements that are within a courseselect element and add their indicies to the ArrayList
for(int i=0; i<numberElementsInNodeList; i++) {
if (nodeList.item(i).getParentNode().getNodeName().equals("courseselect")) {
courseselectItems.add(new Integer(i));
}
}
然后我想稍后检查 ArrayList 是否包含特定索引:
//Cycle through the namedNodeMap array to find each of the course codes
for(int i=0; i<numberElementsInNodeList; i++) {
if(!courseselectItems.contains(new Integer(i))) {
//Do Stuff
}
}
我的问题是,当我使用new Integer(i) 创建一个新整数时,我能否使用ArrayList.contains() 比较整数?也就是说,当我使用new Integer(i)创建一个新对象时,如果用于创建它们的int值相同,那会和之前创建的Integer对象一样吗?
我希望我没有把这说得太清楚。感谢您的帮助!
【问题讨论】:
-
在旁注中,关于整数和字符串有一点值得了解:devx.com/tips/Tip/42276