Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to find indexes of ALL occurrences of object in ArrayList

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find indexes of ALL occurrences of object in ArrayList

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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< (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 donethen return the arraylist containing the indexes
    I hope that made sense.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to find indexes of ALL occurrences of object in ArrayList

    Quote Originally Posted by nafty View Post
    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.

Similar Threads

  1. ArrayList problem, help me to find out the error!
    By jinanzhaorenbo in forum Exceptions
    Replies: 1
    Last Post: July 30th, 2012, 10:55 PM
  2. [SOLVED] how to find all occurrences of a number in an array recursivelly
    By mia_tech in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 01:37 PM
  3. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  4. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 11:09 PM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM