Search Unity

Resolved List : How to get the index number of a particular element?

Discussion in 'Scripting' started by AlanMattano, Nov 23, 2020.

  1. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    I search inside a list.
    I get the item or element
    How to know/get the location index number?

    Code (CSharp):
    1. foreach (DataObject objectElement in database.dataListObject)
    2. {
    3.     if (objectElement.data.Contains(theWordToSearch))
    4.     {
    5.         //int index = DataObject.IndexOf(objectElement); <----------- ???
    6.     }
    7. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Don't use foreach.

    Instead use
    for (i = 0; i < MyList.Count; i++)
    to get the index, and then use that index to dereference each item as you iterate.
     
    Joe-Censored, AlanMattano and Bunny83 like this.
  3. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    yes, that was a stupid questio and a nice answer thx
     
    Bunny83 and Kurt-Dekker like this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you prefer foreach, you'd do it like this

    Code (CSharp):
    1. int index = 0;
    2. foreach (DataObject objectElement in database.dataListObject)
    3. {
    4.     if (objectElement.data.Contains(theWordToSearch))
    5.     {
    6.         //use index here for whatever you need it for
    7.  
    8.  
    9.  
    10.     }
    11.     index++;  //increment index for the next loop
    12. }
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Rather than writing your own loop, you could also use the library function FindIndex.
     
    Joe-Censored likes this.
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah I wrote the same thing (well I suggested IndexOf) and then deleted my post when I saw he has IndexOf already commented out in his/her code. Probably doesn't want slight performance hit of calling IndexOf or FindIndex over and over every iteration of the loop. :p
     
    Antistone likes this.
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Oh. If you're using a loop to do something to every element of the list anyway and you just need to know the index number while you're doing it, then you absolutely should use a for loop.

    Use FindIndex if you only need to know the index of one object out of the entire list, and don't care about the others.
     
    Joe-Censored likes this.
  8. ali_butt

    ali_butt

    Joined:
    Jul 6, 2020
    Posts:
    1
    why not to use foreach, please elaborate a bit
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Did you even read the original question (and the message subject title)from three years ago before you came in necro-posting?

    I'll quote it here for you.

    There are other reasons, such as changing the collection as you iterate it, which is NOT allowed with foreach
     
    AlanMattano and Bunny83 like this.
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,979
    Uhm, you do realise what the question of this thread was, right?

    Of course if you only need a single index you "could" use IndexOf of the collection you're iterating through (if that collection support it), however IndexOf itself has to iterate through the whole collection to find the index. So when using a normal for loop you already have the index at each iteration.

    edit: Damn hadn't refreshed the page so Kurt already answered :)
     
    ali_butt and Kurt-Dekker like this.