📌  相关文章
📜  检查Java中的数组中是否存在值

📅  最后修改于: 2022-05-13 01:55:28.083000             🧑  作者: Mango

检查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

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

方法

有许多方法可以检查此 Array 中是否存在特定Java。这些都是 -

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

1.使用线性搜索方法:

在这种情况下,列表或数组按顺序遍历,并检查每个元素。

句法:

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

例子:

Java
// 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);
    }
}


Java
// 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);
    }
}


Java
// 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);
    }
}


Java
// 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);
    }
}


Java
// 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

2.使用二分查找法:

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

句法:

public static int 
    binarySearch(data_type arr, data_type key)

例子:

Java

// 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

3. 使用List.contains()方法:

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

句法:

public boolean contains(Object)

要搜索的对象元素在哪里。

例子:

Java

// 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

4. 使用Stream.anyMatch()方法:

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

句法:

boolean anyMatch(Predicate 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

// 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

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

Java

// 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