0

I have an ArrayList of type String. I want to determine whether any element of this ArrayList starts with a specified string and if the ArrayList contains this element, then I want to get the index of this element. In addition, I do not want to loop this ArrayList to get the index of that element.

For example :

ArrayList<String> asd = new ArrayList<String>();  // We have an array list

//We filled the array list
asd.add("abcc trtiou");
asd.add("aiwr hiut qwe");
asd.add("vkl: gtr");
asd.add("aAgiur gfjhg ewru");

Now, I want to get the index of the element vkl: gtr by using vkl: without looping array list.(searching also should be case insensitive, so, using vkl: and VkL: should give the index of vkl: gtr)

How can I do this ?

Thanks in advance.

4
  • 1
    ArrayList wasn't designed to support the operations that you're describing. Hopefully you have control here to choose a different data structure that will support your search requirements?
    – sblom
    Commented Apr 26, 2012 at 18:05
  • 1
    @Predoryx no magic here, loops are very much real
    – ant
    Commented Apr 26, 2012 at 18:05
  • To do this without looping you would want to create either some kind of search program that can efficiently go through possible entries. You might want to do more research on what you are trying to do.
    – John Sykor
    Commented Apr 26, 2012 at 18:05
  • It's quite simple: you either loop or call a function that loops. At the bottom there's always a loop. Commented Apr 26, 2012 at 18:09

4 Answers 4

11

You have to loop the ArrayList. You cant possibly access just a single index and be guaranteed it is what you're looking for.

Also, you should consider using another data structure if a lot of searching is involved. Searching an ArrayList takes O(n)time while something like a red-black tree can be done in O(log n).

If you know before program execution the strings used to locate the items in the structure, consider using a HashMap. You can access the items in O(1).

If none of these solutions suit your particular problem expand on your answer with what you're trying to do, we could provide a better answer as to how you'd locate your items with minimal search time.

2

This is as far as you can get with your requirement if you're not looking to perform loop and search against the string objects held in the arraylist.

if(asd.contains("vkl: gtr"))
{
  int index=asd.indexOf("vkl: gtr");
}

or simply:

int index = Arrays.binarySearch(asd.toArray(), 0, asd.size()-1, "vkl: gtr");

If performing loop in your calling method is what you're looking to avoid then, alternative you can create a class which extends ArrayList and have a method which does the index lookup.

  class MyArray extends ArrayList<String>
  {
    public int getIndexOf(String o)
    {
      for (int i = 0; i < size(); i++)
      {
        if (get(i).contains((String) o)) return i;
      }
      return -(size() - 1);
    }
  }

Then from your calling program do:

public void foo()
{
    MyArray asd = new MyArray();
    asd.add("abcc trtiou");
    asd.add("aiwr hiut qwe");
    asd.add("vkl: gtr");
    asd.add("aAgiur gfjhg ewru");

    int index = asd.getIndexOf("vkl:");
}
1
  • @Bitmap Now, I want to get the index of the element vkl: gtr by using vkl: you assume he has full string
    – ant
    Commented Apr 26, 2012 at 18:16
1
for(int i=0; i < asd.size(); i++) {
    String s = asd.get(i);
    //search the string
    if(found) {
        return i
    }
}
return -1
0

I don't really understand if you are looking for something like key-value pairs or single string entry search. If you are looking for the first one you should use Map instead of a simple array if you want to search for a key Here you can put a pair using

put(Object key, Object value) 

and the getting the value of a specified key with

get(Object key) 

If you are looing only for a quick way of finding a part of string into an array you have to read all indexes and compare strings one by one using stringToCompare.equalsIgnoreCase(otherStringToCompare). Note that this will throw an exception if stringToCompare is NULL

Not the answer you're looking for? Browse other questions tagged or ask your own question.