How to find indexes of ALL occurrences of object in ArrayList
Hi,
I am interested to find all the indexes where an element that I specify occurs in an ArrayList. Is there already a method for this? I see in the documentation that there are two similar methods that return the index of the first appearance of the element (indexOf) and the last index of (lastIndexOf). Is there a method to return all indexes? Am I missing something? Or do I need to write my own code to achieve this?
On a probably-separate note: what is an Iterator? Does that have anything to do with this?
Basically what I want is to have returned to me a list of all the indexes where an object appears in a list, e.g. <pre> List<Elements> foundElements = variableArrayList.getAllIndexesOf(varElement);</pre>
Thanks much,
-Daniel
Re: How to find indexes of ALL occurrences of object in ArrayList
Quote:
do I need to write my own code to achieve this?
If you don't see a method, you'll probably need to write your own code.
Re: How to find indexes of ALL occurrences of object in ArrayList
Okay so the collections class doesnt have a method for this, you can however iterate through the whole list and check each element and store each index of the occurrence in a separate arraylist. Even if the collections class had this method it would also have to iterate through it, so a pseudocode for this could be:
PHP Code:
for(int i = 0; i < (length of the list to be checked); i++)
{
if(object at index i is = to the object to be checked)
{
then add its index(or i in this case) to a temporary arraylist.
}
}
After everything in the loop is done, then return the arraylist containing the indexes.
I hope that made sense.
Re: How to find indexes of ALL occurrences of object in ArrayList
@matinm90 If you are going to post code, you should properly format it. Nested statements should be indented to make the code easier to read and understand. All statements should NOT start in the first column.
Re: How to find indexes of ALL occurrences of object in ArrayList
Sorry I am new to posting code on a forum, I will fix it.
Re: How to find indexes of ALL occurrences of object in ArrayList
Quote:
Originally Posted by
nafty
Hi,
I am interested to find all the indexes where an element that I specify occurs in an ArrayList. Is there already a method for this? I see in the documentation that there are two similar methods that return the index of the first appearance of the element (indexOf) and the last index of (lastIndexOf). Is there a method to return all indexes? Am I missing something? Or do I need to write my own code to achieve this?
On a probably-separate note: what is an Iterator? Does that have anything to do with this?
Basically what I want is to have returned to me a list of all the indexes where an object appears in a list, e.g. <pre> List<Elements> foundElements = variableArrayList.getAllIndexesOf(varElement);</pre>
Thanks much,
-Daniel
A Iterator is just an manner to iterate over a collection. When you look to the API it has no methods to say when a element is in the collection.