Home > Java > JavaBase > How to determine whether an element is in an array in java

How to determine whether an element is in an array in java

Release: 2019-11-21 16:52:35
Original
12826 people have browsed it

How to determine whether an element is in an array in java

How to determine whether an element exists in an array in java:

1. Convert the array into a List, and then use the contains in the List to determine whether it exists.

public static boolean useList(String[] arr,String containValue){
    return Arrays.asList(arr).contains(containValue);
  }
Copy after login

It should be noted that the List converted in the Arrays.asList method is not java.util.ArrayList but java.util.Arrays.ArrayList. Arrays cannot be converted in java.util.Arrays.ArrayList. The length is particularly important for expansion operations. The contains implementation is as follows:

@Override
public boolean contains(Object o) {
  //调用indexOf方法判断其在那个位置,判断其时候为-1
   return indexOf(o) != -1;
}
@Override
public int indexOf(Object o) {
  //获取元素
  E[] a = this.a;
  //判断空
  if (o == null) {
    //循环判断
    for (int i = 0; i < a.length; i++)
      //如果元素为null
      if (a[i] == null)
       //则返回
       return i;
  } else {
    //如果其不为空
    for (int i = 0; i < a.length; i++)
     //判断元素与a[i]是否相等
      if (o.equals(a[i]))
       //相等返回i
       return i;
  }
      //否则返回-1
      return -1;
}
Copy after login

2. Use Set to implement to determine whether it exists

public static boolean useSet(String[] arr,String containValue){
    return new HashSet<>(Arrays.asList(arr)).contains(containValue);
  }
Copy after login

3. Use loops to implement

public static boolean useLoop(String[] arr,String containValue){
    //判断是否为空
    if (arr==null||arr.length==0){
      return false;
    }
    for (int i = 0; i < arr.length; i++) {
      //all null
      if (containValue!=null&&containValue.equals(arr[i])){
        return true;
      }else if (arr[i]==null){
        return true;
      }
    }
    return false;
  }
Copy after login

4. Use the contains method in org.apache.commons.lang3.ArrayUtils to implement

public static boolean useUtils(String[] arr,String containValue){
    return ArrayUtils.contains(arr,containValue);
  }
Copy after login

Implementation code:

public static boolean contains(final Object[] array, final Object objectToFind) {
    //调用indexof进行判断位置
    return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
  }
  public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
    //判断null
    if (array == null) {
      return INDEX_NOT_FOUND;
    }
    //判断起始位置
    if (startIndex < 0) {
      startIndex = 0;
    }
    //判断查询元素是否为null
    if (objectToFind == null) {
      //null则直接使用==进行循环判断位置
      for (int i = startIndex; i < array.length; i++) {
        if (array[i] == null) {
          return i;
        }
      }
     //判断元素是不是array中的元素的实例,如果是则循环并采用equals进行判断
    } else if (array.getClass().getComponentType().isInstance(objectToFind)) {
      for (int i = startIndex; i < array.length; i++) {
        if (objectToFind.equals(array[i])) {
          return i;
        }
      }
    }
    //返回没有找到
    return INDEX_NOT_FOUND;
  }
Copy after login

For more java knowledge, please pay attention to java basic tutorial .

The above is the detailed content of How to determine whether an element is in an array in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template