如何在android中检查arraylist是否包含这个特定的词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21724948/
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
How to check arraylist contains this particular word in android?
提问by anddev
I have ArrayList<String>
in that I added 3-4 website names. Like, http://www.google.com, https://www.stackoverflow.com, etc. Now in my application if I type simply "google" then I want to compare that "google" word with the ArrayList<String>
.
我ArrayList<String>
在其中添加了 3-4 个网站名称。例如,http://www.google.com、https://www.stackoverflow.com等。现在在我的应用程序中,如果我只输入“google”,那么我想将该“google”字与ArrayList<String>
.
I am stuck here. Can anyone tell me how can I compare the string with the array object?
我被困在这里。谁能告诉我如何将字符串与数组对象进行比较?
Thanks in advance.
提前致谢。
采纳答案by Pankaj Kumar
To do so, you need to override implementation of contains()
. I am giving you a simple example.
为此,您需要覆盖contains()
. 我给你举个简单的例子。
Custom ArrayList class
自定义 ArrayList 类
public class MyArrayList extends ArrayList<String> {
private static final long serialVersionUID = 2178228925760279677L;
@Override
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
@Override
public int indexOf(Object o) {
int size = this.size();
if (o == null) {
for (int i = 0; i < size ; i++) {
if (this.get(i) == null) {
return i;
}
}
} else {
for (int i = 0; i < size ; i++) {
if (this.get(i).contains(String.valueOf(o))) {
return i;
}
}
}
return -1;
}
}
How to use
如何使用
MyArrayList arrayList = new MyArrayList();
arrayList.add("http://www.google.com");
arrayList.add("https://www.stackoverflow.com");
arrayList.add("http://pankajchunchun.wordpress.com");
if (arrayList.contains("google")) {
System.out.println("ArrayList Contains google word");
}
if (arrayList.contains("igoogle")) {
System.out.println("ArrayList Contains igoogle word");
} else {
System.out.println("ArrayList does not Contains igoogle word");
}
Below is output for above code example
以下是上述代码示例的输出
ArrayList Contains google word
ArrayList does not Contains igoogle word
See ArrayList Source Codefor more custom implementation.
有关更多自定义实现,请参阅ArrayList 源代码。
回答by Blackbelt
ArrayList.contains()
test the the String through equals. From the documentation:
ArrayList.contains()
通过equals测试字符串。从文档:
public boolean contains(Object o) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
public boolean contains(Object o) 如果此列表包含指定的元素,则返回 true。更正式地说,当且仅当此列表包含至少一个元素 e 使得 (o==null ? e==null : o.equals(e)) 时才返回 true。
example:
例子:
boolean contains = yourArrayListInstance.contains(yourString);
Edit. If you want to check for substring you have to loop on the ArrayList's content and call String.contains
编辑。如果要检查子字符串,则必须循环访问 ArrayList 的内容并调用String.contains
回答by Apoorv
You can iterate through your ArrayList<String>
like
你可以遍历你ArrayList<String>
喜欢的
public String getWebsiteName(String toMatchString)
{
ArrayList<String> yourArrayList = new ArrayList<String>();
for (String webSiteName : yourArrayList)
{
if (webSiteName.contains(toMatchString))
return webSiteName;
}
return null;
}
and get the matching String
并获得匹配 String
回答by FD_
Use a helper function like this
使用这样的辅助函数
public static boolean containsSubString(ArrayList<String> stringArray, String substring){
for (String string : stringArray){
if (string.contains(substring)) return true;
}
return false;
}