[英]How to check whether a String is present in the Custom Array or not?
I have an array of private SearchAutoSuggestResult[] mArrSearchAutoSuggest;
我有一组
private SearchAutoSuggestResult[] mArrSearchAutoSuggest;
where the Cusom class is Cusom类在哪里
public class SearchAutoSuggestResult
{
private String did;
private String Res;
public String getDid ()
{
return did;
}
public void setDid (String did)
{
this.did = did;
}
public String getRes ()
{
return Res;
}
public void setRes (String Res)
{
this.Res = Res;
}
@Override
public String toString()
{
return "ClassPojo [did = "+did+", Res = "+Res+"]";
}
}
How i can check String Res lets say "Hello" is present in the array and at what position. 我如何检查String Res,可以说“ Hello”出现在数组中以及位于什么位置。
first, convert your array to arraylist, then check if expected string exists in that arraylist as below: 首先,将数组转换为arraylist,然后检查该arraylist中是否存在预期的字符串,如下所示:
public boolean isKeyExists(String array[], String key) {
List<String> myList = new ArrayList<String>(Arrays.asList(array));
return myList.contains(key);
}
You will need to loop through your array: 您将需要遍历数组:
for(int i = 0; i < mArrSearchAutoSuggest.length; i++) {
SearchAutoSuggestResult result = mArrSearchAutoSuggest[i];
String res = result.getRes();
if(res != null && res.equals("Hello") { //means the result exists ans its value is "Hello"
System.out.print(res);
System.out.println(i); //prints the position of result in the array
}
}
You can use the Arrays class to search whether an element is present in the array. 您可以使用Arrays类来搜索数组中是否存在元素。
Arrays.binarySearch(array[],key);
it returns the index of given key in the given array if present otherwise it returns -1. 它返回给定键在给定数组中的索引(如果存在),否则返回-1。 Make sure your bean has implemented
hashcode()
and equals()
method. 确保您的bean实现了
hashcode()
和equals()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.