检查元素是否存在于数组中

时间:2022-09-26 07:42:43

A simple Java code for checking whether an element exists in an array or not:

用于检查数组中是否存在元素的简单Java代码:

import java.util.Arrays;

public class Main {
    static int[] numbers = {813, 907, 908, 909, 910};

    public static void main(String[] args) {
        int number = 907;
        //Integer number = 907; // the same thing -- it's not found.
        boolean b = Arrays.asList(numbers).contains(number);
        System.out.println(b);  // => false
    }
}

1) Why doesn't it find 907 in the array?

1)为什么在数组中找不到907 ?

2) If there is a better way of doing it, go ahead and share your knowledge.

如果有更好的方法,那就去分享你的知识吧。

UPDATE:

更新:

It was said that asList converts your int[] into a List<int[]> with a single member: the original list. However, I expect the following code to give me 1, but it gives me 5:

据说asList将您的int[]转换为一个只有一个成员的列表 :原始列表。然而,我期望下面的代码给我1,但它给我5: []>

System.out.println(Arrays.asList(numbers).size());

6 个解决方案

#1


12  

The problem is that Arrays.asList(numbers) isn't doing what you think. It is converting your int[] into a List<int[]> with a single member: the original list.

问题是Arrays.asList(数字)并没有按照你的想法去做。它将您的int[]转换为一个列表 ,其中有一个成员:原始列表。 []>

You can do a simple linear search or, if your numbers array is always sorted, use Arrays.binarySearch(numbers, 907); and test whether the result is negative (meaning not found).

你可以做一个简单的线性搜索,或者,如果你的数字数组总是被排序的话,使用数组。binarySearch(数字,907);测试结果是否为阴性(即未发现)。

#2


6  

Lists don't contain primitives, so Arrays.asList(int[]) will produce a List with one entry of type int[].

列表不包含原语,因此Arrays.asList(int[])将生成一个包含一个类型为int[]的条目的列表。

This code works:

这段代码:

static Integer[] numbers = {813, 907, 908, 909, 910};

public static void main(String[] args) {
    Integer number = 907;
    boolean b = Arrays.asList(numbers).contains(number);
    System.out.println(b);  // => false
}

For your question as what will Arrays.asList(numbers) contain as long as it is an int[]:

对于你的问题,什么是Arrays.asList(数),只要它是int[]:

This code:

这段代码:

static int[] numbers = {813, 907, 908, 909, 910};

public static void main(String[] args) {
    int number = 907;
    List<int[]> list = Arrays.asList(numbers);

    boolean b = list.contains(number);
    System.out.println(b);  // => false
    System.out.println("list: " + list);
    for(int[] next : list) {
        System.out.println("content: " + Arrays.toString(next));
    }
}

has this result:

有这样的结果:

false
list: [[I@da89a7]
content: [813, 907, 908, 909, 910]

As you can see, the list contains one element of type int[] (the [[I indicate the int[]). It has the elements that were initially created.

如您所见,列表包含一个int[]类型的元素([I指示int[])。它包含最初创建的元素。

#3


2  

With guava ImmutableSet:

番石榴ImmutableSet:

public class Main {

    private static final Set<Integer> NUMBERS = ImmutableSet.of(813, 907, 908, 909, 910);

    public static void main(final String[] args) {
        final int number = 907;
        final boolean b = NUMBERS.contains(number);
        System.out.println(b); // true
    }
}

ImmutableSet ensures no one adds something to NUMBERS

immutabet确保没有人向数字添加内容

#4


1  

Since your array is sorted, you can use Arrays.binarySearch().

由于数组已经排序,所以可以使用Arrays.binarySearch()。

This allows you not to have to convert to a List first. Check the return code of this method: if it is positive, the element is in the array; if it is negative, it isn't:

这允许您不必首先转换为列表。检查此方法的返回代码:如果是正数,则元素在数组中;如果它是负数,它不是:

int number = 907;
System.out.println(Arrays.binarySearch(numbers, number) >= 0);

#5


0  

Use this code instead. This is just one of those times you've got to deal with Java's strongly typed quirks :)

使用这段代码。这只是你需要处理Java强类型怪癖的其中一个例子。

import java.util.Arrays;

    public class Main {
        static Integer[] numbers = {813, 907, 908, 909, 910};

        public static void main(String[] args) {
        Integer number = new Integer(907);

        boolean b = Arrays.asList(numbers).contains(number);
        System.out.println(b); 
    }
}

#6


0  

You could just use a for loop going through the array and match each element with the one that you are looking for. Afterwards you can go from there.

您可以使用一个for循环遍历数组并将每个元素与您要查找的元素匹配。然后你可以从那里出发。

For the reason why it isnt working is the same reason as others said before.

因为它不工作的原因和其他人之前说的一样。

#1


12  

The problem is that Arrays.asList(numbers) isn't doing what you think. It is converting your int[] into a List<int[]> with a single member: the original list.

问题是Arrays.asList(数字)并没有按照你的想法去做。它将您的int[]转换为一个列表 ,其中有一个成员:原始列表。 []>

You can do a simple linear search or, if your numbers array is always sorted, use Arrays.binarySearch(numbers, 907); and test whether the result is negative (meaning not found).

你可以做一个简单的线性搜索,或者,如果你的数字数组总是被排序的话,使用数组。binarySearch(数字,907);测试结果是否为阴性(即未发现)。

#2


6  

Lists don't contain primitives, so Arrays.asList(int[]) will produce a List with one entry of type int[].

列表不包含原语,因此Arrays.asList(int[])将生成一个包含一个类型为int[]的条目的列表。

This code works:

这段代码:

static Integer[] numbers = {813, 907, 908, 909, 910};

public static void main(String[] args) {
    Integer number = 907;
    boolean b = Arrays.asList(numbers).contains(number);
    System.out.println(b);  // => false
}

For your question as what will Arrays.asList(numbers) contain as long as it is an int[]:

对于你的问题,什么是Arrays.asList(数),只要它是int[]:

This code:

这段代码:

static int[] numbers = {813, 907, 908, 909, 910};

public static void main(String[] args) {
    int number = 907;
    List<int[]> list = Arrays.asList(numbers);

    boolean b = list.contains(number);
    System.out.println(b);  // => false
    System.out.println("list: " + list);
    for(int[] next : list) {
        System.out.println("content: " + Arrays.toString(next));
    }
}

has this result:

有这样的结果:

false
list: [[I@da89a7]
content: [813, 907, 908, 909, 910]

As you can see, the list contains one element of type int[] (the [[I indicate the int[]). It has the elements that were initially created.

如您所见,列表包含一个int[]类型的元素([I指示int[])。它包含最初创建的元素。

#3


2  

With guava ImmutableSet:

番石榴ImmutableSet:

public class Main {

    private static final Set<Integer> NUMBERS = ImmutableSet.of(813, 907, 908, 909, 910);

    public static void main(final String[] args) {
        final int number = 907;
        final boolean b = NUMBERS.contains(number);
        System.out.println(b); // true
    }
}

ImmutableSet ensures no one adds something to NUMBERS

immutabet确保没有人向数字添加内容

#4


1  

Since your array is sorted, you can use Arrays.binarySearch().

由于数组已经排序,所以可以使用Arrays.binarySearch()。

This allows you not to have to convert to a List first. Check the return code of this method: if it is positive, the element is in the array; if it is negative, it isn't:

这允许您不必首先转换为列表。检查此方法的返回代码:如果是正数,则元素在数组中;如果它是负数,它不是:

int number = 907;
System.out.println(Arrays.binarySearch(numbers, number) >= 0);

#5


0  

Use this code instead. This is just one of those times you've got to deal with Java's strongly typed quirks :)

使用这段代码。这只是你需要处理Java强类型怪癖的其中一个例子。

import java.util.Arrays;

    public class Main {
        static Integer[] numbers = {813, 907, 908, 909, 910};

        public static void main(String[] args) {
        Integer number = new Integer(907);

        boolean b = Arrays.asList(numbers).contains(number);
        System.out.println(b); 
    }
}

#6


0  

You could just use a for loop going through the array and match each element with the one that you are looking for. Afterwards you can go from there.

您可以使用一个for循环遍历数组并将每个元素与您要查找的元素匹配。然后你可以从那里出发。

For the reason why it isnt working is the same reason as others said before.

因为它不工作的原因和其他人之前说的一样。