简体   繁体   English

如何检查自定义数组中是否存在字符串?

[英]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.

相关问题 如何在java中检查Object是String还是String Array? - How to check whether an Object is String or String Array in java? 如何检查给定的字符串数组是否返回null? - How to check whether given string array returns null? 如何检查 JSON 数组的所有元素是否以特定字符串开头? - How to check whether all elements of a JSON array start with a specific string? 如何检查给定对象是JSON字符串中的对象还是数组 - How to check whether the given object is object or Array in JSON string 检查特定字符串是否存在于主机名中的最佳方法是什么? - What is the best way to check whether a particular string is present in a hostname or not? 给定一个整数数组,检查给定产品是否存在两个数字 - Given an array of integers check whether there are two numbers present with given product 如何检查一个字符串是否是回文? - How to check whether a string is palindrome or not? 如何在Android Studio中检查英语词典中是否存在单词? - How to check whether a word is present in English Dictionary or not in Android Studio? 如何检查值是否不存在或没有值? - How to check whether value is not present or just has no value? java - 如何检查列表中是否存在列表 - How to check whether list is present inside list in the java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM