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); }
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; }
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); }
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; }
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); }
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; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software