Java ArrayList 搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10338894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ArrayList search
提问by Predoryx
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.
我有一个ArrayList
类型String
。我想确定这个元素是否ArrayList
以指定的字符串开头,如果ArrayList
包含这个元素,那么我想获取这个元素的索引。另外,我不想循环这个ArrayList
来获取该元素的索引。
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
)
现在,我想vkl: gtr
通过使用vkl:
而不使用循环数组列表来获取元素的索引。(搜索也应该不区分大小写,因此,使用vkl:
和VkL:
应该给出的索引vkl: gtr
)
How can I do this ?
我怎样才能做到这一点 ?
Thanks in advance.
提前致谢。
回答by Aidanc
You have to loop the ArrayList. You cant possibly access just a single index and be guaranteed it is what you're looking for.
您必须循环 ArrayList。您不可能只访问一个索引并保证它就是您要查找的索引。
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)
.
此外,如果涉及大量搜索,您应该考虑使用其他数据结构。搜索 ArrayList 需要O(n)
时间,而在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)
.
如果您在程序执行之前知道用于定位结构中项目的字符串,请考虑使用 HashMap。您可以访问 中的项目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.
如果这些解决方案中没有一个适合您的特定问题,并扩展了您尝试执行的操作的答案,我们可以提供更好的答案,说明您如何以最少的搜索时间找到您的项目。
回答by Bitmap
This is as far asyou can get with your requirement if you're not looking to perform loop and search against the string objects held in the arraylist.
这是只要你可以与你的要求得到,如果你不希望进行循环,并针对搜索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.
如果在您的调用方法中执行循环是您想要避免的,那么您可以创建一个扩展类ArrayList
并具有一个执行索引查找的方法。
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:");
}
回答by MarioDS
for(int i=0; i < asd.size(); i++) {
String s = asd.get(i);
//search the string
if(found) {
return i
}
}
return -1
回答by PierPier
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
我真的不明白您是否正在寻找诸如键值对或单字符串条目搜索之类的东西。如果你正在寻找第一个你应该使用 Map 而不是一个简单的数组如果你想搜索一个键在这里你可以使用
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
如果您只想快速找到字符串的一部分到数组中,则必须读取所有索引并使用 stringToCompare.equalsIgnoreCase(otherStringToCompare) 逐个比较字符串。请注意,如果 stringToCompare 为 NULL,这将引发异常