Reputation: 51
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.
Upvotes: 0
Views: 14178
Reputation: 12538
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:");
}
Upvotes: 2
Reputation: 13073
for(int i=0; i < asd.size(); i++) {
String s = asd.get(i);
//search the string
if(found) {
return i
}
}
return -1
Upvotes: 1
Reputation: 1
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
Upvotes: 0
Reputation: 7011
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.
Upvotes: 11