Compare new Integer Objects in ArrayList Question

Posted by thechiman on Stack Overflow See other posts from Stack Overflow or by thechiman
Published on 2010-04-13T23:24:09Z Indexed on 2010/04/13 23:33 UTC
Read the original article Hit count: 472

Filed under:
|
|

I am storing Integer objects representing an index of objects I want to track. Later in my code I want to check to see if a particular object's index corresponds to one of those Integers I stored earlier. I am doing this by creating an ArrayList and creating a new Integer from the index of a for loop:

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));
    }
}

I then want to check later if the ArrayList contains a particular index:

//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
    }
}

My question is, when I create a new Integer by using new Integer(i) will I be able to compare integers using ArrayList.contains()? That is to say, when I create a new object using new Integer(i), will that be the same as the previously created Integer object if the int value used to create them are the same?

I hope I didn't make this too unclear. Thanks for the help!

© Stack Overflow or respective owner

Related posts about java

Related posts about integer