Java 检查一个值是否存在于一个数组中

Java 检查一个值是否存在于一个数组中

给定一个数组,任务是编写一个Java程序来检查一个特定的元素是否存在于这个数组中。

例子

Input: arr[] = [5, 1, 1, 9, 7, 2, 6, 10], key = 7
Output: true

Input: arr[] = [-1, 1, 5, 8], key = -2
Output: false

数组是一个包含一组元素的数据结构。通常,这些元素都是相同的数据类型,如整数或字符串。数组在计算机程序中通常用于组织数据,以便对一组相关的数值进行快速排序或搜索。阵列的所有项目都存储在连续的内存位置。

方法

在Java中,有许多方法来检查一个特定的元素是否存在于这个数组中。这些方法是–

  • 使用线性搜索法
  • 使用二进制搜索方法
  • 使用List.contains()方法
  • 使用Stream.anyMatch()方法

1.使用线性搜索法

在这种情况下,列表或数组被依次遍历,每个元素都被检查。

语法

for (int element : arr) {
    if (element == toCheckValue) {
        return true;
    }
}

例子

// Java program to check whether
// an element is present in array or not
 
import java.util.Arrays;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function return true if given element
    // found in array
    private static void check(int[] arr, int toCheckValue)
    {
        // check if the specified element
        // is present in the array or not
        // using Linear Search method
        boolean test = false;
        for (int element : arr) {
            if (element == toCheckValue) {
                test = true;
                break;
            }
        }
 
        // Print the result
        System.out.println("Is " + toCheckValue
                           + " present in the array: " + test);
    }
 
    public static void main(String[] args)
    {
 
        // Get the array
        int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
 
        // Get the value to be checked
        int toCheckValue = 7;
 
        // Print the array
        System.out.println("Array: "
                           + Arrays.toString(arr));
 
        // Check if this value is
        // present in the array or not
        check(arr, toCheckValue);
    }
}

输出

Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true

时间复杂度: O(N)

辅助空间: O(1)

2.使用二进制搜索法

在这个方法中,通过重复地将搜索区间分成两半来搜索一个排序的数组。从一个覆盖整个数组的区间开始。如果搜索键的值小于区间中间的项目,则将区间缩小到下半部分。否则,将其缩小到上半部分。反复检查,直到找到该值或者区间为空。
在这个例子中,Arrays.binarySearch()方法被用于二进制搜索。

语法

public static int 
    binarySearch(data_type arr, data_type key)

例子

// Java program to check whether
// an element is present in array or not
 
import java.util.Arrays;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function return true if given element
    // found in array
    private static void check(int[] arr, int toCheckValue)
    {
        // sort given array
        Arrays.sort(arr);
 
        // check if the specified element
        // is present in the array or not
        // using Binary Search method
        int res = Arrays.binarySearch(arr, toCheckValue);
 
        boolean test = res >= 0 ? true : false;
 
        // Print the result
        System.out.println("Is " + toCheckValue
                           + " present in the array: " + test);
    }
 
    public static void main(String[] args)
    {
 
        // Get the array
        int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
 
        // Get the value to be checked
        int toCheckValue = 7;
 
        // Print the array
        System.out.println("Array: "
                           + Arrays.toString(arr));
 
        // Check if this value is
        // present in the array or not
        check(arr, toCheckValue);
    }
}

输出

Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true

时间复杂度: O(nlog(n))

辅助空间: O(1)

3.使用List.contains()方法

Java中的List contains()方法用于检查指定元素是否存在于给定的列表中。

语法

public boolean contains(Object)

其中要搜索的对象-元素。

例子

// Java program to check whether
// an element is present in array or not
 
import java.util.Arrays;
 
class GFG {
 
    // Function return true if given element
    // found in array
    private static void check(Integer[] arr, int toCheckValue)
    {
        // check if the specified element
        // is present in the array or not
        // using contains() method
        boolean test
            = Arrays.asList(arr)
                  .contains(toCheckValue);
 
        // Print the result
        System.out.println("Is " + toCheckValue
                           + " present in the array: " + test);
    }
 
    public static void main(String[] args)
    {
 
        // Get the array
        Integer arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
 
        // Get the value to be checked
        int toCheckValue = 7;
 
        // Print the array
        System.out.println("Array: "
                           + Arrays.toString(arr));
 
        // Check if this value is
        // present in the array or not
        check(arr, toCheckValue);
    }
}

输出

Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true

时间复杂度: O(N)

辅助空间: O(1)

4.使用Stream.anyMatch()方法

Stream anyMatch(Predicate predicate)返回该流的任何元素是否与提供的谓词匹配。如果不是为了确定结果,它可能不会在所有元素上评估该谓词。

语法

boolean anyMatch(Predicate<T> predicate)

Where T is the type of the input to the predicate
and the function returns true if any elements of
the stream match the provided predicate, 
otherwise false.

例1: 使用Stream.of()方法创建Stream

// Java program to check whether
// an element is present in array or not
 
import java.util.Arrays;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function return true if given element
    // found in array
    private static void check(int[] arr, int toCheckValue)
    {
        // check if the specified element
        // is present in the array or not
        // using anyMatch() method
        boolean test
            = IntStream.of(arr)
                  .anyMatch(x -> x == toCheckValue);
 
        // Print the result
        System.out.println("Is " + toCheckValue
                           + " present in the array: " + test);
    }
 
    public static void main(String[] args)
    {
 
        // Get the array
        int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
 
        // Get the value to be checked
        int toCheckValue = 7;
 
        // Print the array
        System.out.println("Array: "
                           + Arrays.toString(arr));
 
        // Check if this value is
        // present in the array or not
        check(arr, toCheckValue);
    }
}

输出

Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true

时间复杂度: O(N)

辅助空间: O(1)

例2: 使用Arrays.stream()方法来创建Stream

// Java program to check whether
// an element is present in array or not
 
import java.util.Arrays;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function return true if given element
    // found in array
    private static void check(int[] arr, int toCheckValue)
    {
        // check if the specified element
        // is present in the array or not
        // using anyMatch() method
        boolean test
            = IntStream.of(arr)
                  .anyMatch(x -> x == toCheckValue);
 
        // Print the result
        System.out.println("Is " + toCheckValue
                           + " present in the array: " + test);
    }
 
    public static void main(String[] args)
    {
 
        // Get the array
        int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
 
        // Get the value to be checked
        int toCheckValue = 7;
 
        // Print the array
        System.out.println("Array: "
                           + Arrays.toString(arr));
 
        // Check if this value is
        // present in the array or not
        check(arr, toCheckValue);
    }
}

输出

Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true

时间复杂度: O(N)

辅助空间: O(1)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程