Java ArrayList search

♀尐吖头ヾ 提交于 2019-12-25 08:47:47

问题


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.


回答1:


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:");
}



回答3:


for(int i=0; i < asd.size(); i++) {
    String s = asd.get(i);
    //search the string
    if(found) {
        return i
    }
}
return -1



回答4:


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



来源:https://stackoverflow.com/questions/10338894/java-arraylist-search

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!