Array Indexes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charindal
    New Member
    • Dec 2009
    • 17

    #1

    Array Indexes

    i would like some help on how to retrieve array indexes. i have the following scenario:

    List <string> alphabet = new ....;

    alphabet.add("a ");
    alphabet.add("b ");
    alphabet.add("a ");
    alphabet.add("c ");
    alphabet.add("a ");

    i have populated a new arraylist which only identifies distinct/unique alphabet items in the firt array e.g. it is able to list contents a, b and c only.

    my challenge is how do i identify occurence index for the unique array list so that it displays and shows that
    item "a" exists on index 0,2,4
    item "b" exists on index 1
    item "c" exists on index 3

    thank you..
  • adwenger0066
    New Member
    • Aug 2010
    • 4

    #2
    Code:
    public int[] indexesOf(List list, String string) {
      List<int> result = new List<int>();
      for (int i = 0; i < list.Count; i++) { 
       if (list[i] == string) {
            result.Add(i);
       }
      }
      return result.ToArray();
    }

    Comment

    Working...