Sorry, but all these code fragments, without the actual data, don't really help.
I suggest you put a few print statements into your for loop to confirm the actual contents of the list and all the working values as the loop is iterated.

Not sure if the "iterator" is the problem for you. How about modify your code to use a simple loop instead of the "iterator"? You want the index of "or" which is the same as iterating through a loop using real integer.

Not sure if the "iterator" is the problem for you. How about modify your code to use a simple loop instead of the "iterator"? You want the index of "or" which is the same as iterating through a loop using real integer.

My guess is that the indexOf() method is counting the position starting from whatever position the "iterator" returns. Because you are using the first item returned by the "iterator." the value of indexOf() is always 0 (the first position). This could be an intention for indexOf() method? I don't know...

My guess is that the indexOf() method is counting the position starting from whatever position the "iterator" returns. Because you are using the first item returned by the "iterator." the value of indexOf() is always 0 (the first position). This could be an intention for indexOf() method? I don't know...

By the way, please mark it as solved.

So, what's the point of using indexOf method or what's the point of giving this kind of method? Is there any other method which can directly give me the index?

I was going to mark it as solved but as my thread title is about getting a solution of the indexOf method of ArraList, so I've not marked it. Okay, I'm marking it. But please look that matter also. Thnkx.

The indexOf() method is a good method to use without calling iterator. However, you need to apply the method correctly for your own use. The API doc describes what to expect from using it; however, it fails to explain about the "iterator" situation.

ArrayList<String> a = new ArrayList<String>();
a.add("Y"); a.add("A"); a.add("A"); a.add("R"); a.add("R"); // store "YAARR"
// You cannot get the 2nd appearance of "A" using indexOf() because it always returns
// the first occurrence found
System.out.println(a.indexOf("A")); // print 1
// But it is a useful method if you want to randomly check for
// object that may be in the list without going through the whole list
if (a.indexOf("R")>=0) {
System.out.println("Contains 'R'");
}
else {
System.out.println("No 'R'");
}

The indexOf() method is a good method to use without calling iterator. However, you need to apply the method correctly for your own use. The API doc describes what to expect from using it; however, it fails to explain about the "iterator" situation.

ArrayList<String> a = new ArrayList<String>();
a.add("Y"); a.add("A"); a.add("A"); a.add("R"); a.add("R"); // store "YAARR"
// You cannot get the 2nd appearance of "A" using indexOf() because it always returns
// the first occurrence found
System.out.println(a.indexOf("A")); // print 1
// But it is a useful method if you want to randomly check for
// object that may be in the list without going through the whole list
if (a.indexOf("R")>=0) {
System.out.println("Contains 'R'");
}
else {
System.out.println("No 'R'");
}

Ya. I know this thing. but I understand that somehow it's not working for me in this code.

Ya. I know this thing. but I understand that somehow it's not working for me in this code.

Great. :) So there is nothing wrong with the indexOf() method. It is just that you use the method differently and get unexpected result because of missing documentation.

I think it is a good lesson to learn about implementing and documenting a class which are used by others. Sometimes, you, who is the class implementor, do not expect others to use your class in such a way that could give unexpected results. As a result, you do not document it. In this case, I guess that the ArrayList implementor does not expect others to use it the way you do here, so there is no documentation about it.

Hi Taywin.
Forgive me if this is a dumb question, but what exactly is the special problem with iterators and indexOf that's not documented? All I can see here is that indexOf only returns the index of the first occurrence, whether that's being done in an iterator or not, which is clearly documented:

public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list...

If I were going to iterate through a collection and wanted to keep the index, I would never use the iterator. I guess that what the class implementor expects as well because it makes sense. So the documentation about iterator involve in indexOf() is not there.

If I were going to iterate through a collection and wanted to keep the index, I would never use the iterator. I guess that what the class implementor expects as well because it makes sense. So the documentation about iterator involve in indexOf() is not there.

Can't get you properly. And no idea of documenting something. I only know Java Documentation.
and for the code part I think