Java, Simplified Check If Int Array Contains Int

Java, Simplified check if int array contains int

Here is Java 8 solution

public static boolean contains(final int[] arr, final int key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}

Check an Array if it contains an integer in Java

You can use IntStream in Java 8

if (IntStream.of(field).anyMatch(i -> i == 1) &&
IntStream.of(field).anyMatch(i -> i == 2)) {
// has a 1 and a 2

Checking if an array contains certain integers

Arrays.asList(perm).contains(num) 

from How can I test if an array contains a certain value?

for (int i = 0; i < perm.length; i++)

this is not enough to loop like this, if collision happens some slots would have been not initalized.

Overall, for this task you better initialize array with values in order and then shuffle it by using random permutation indexes

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue)

Warning: this doesn't work for arrays of primitives (see the comments).


Since java-8 you can now use Streams.

String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);

To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.

Example

int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);

How can I find if my ArrayList of int[] contains an int[]

A list of arrays stores only the references to the arrays, not their content, therefore contains() is only able to say, if the list contains the exact reference to the array you input. It doesn't evaluate the contents of the array.

If you need to check, if the list contains an array with specific values, you will have to iterate over the list and use Arrays.equals(arr1, arr2).

How can I use .contains method to check if integer value is in an two dimensional array?

The method contains() is valid for Collections, not for Array.

In order to use this method, you need to convert your arrays into Collections first. As you have a two-dimensional array, I suggest you loop through it and analyze each of its one-dimensional subarrays to look for the desired value.

public boolean containsValue(int[][] data, int z) {
for (int i = 0; i < data.length; i++) {
if (Arrays.asList(data[i]).contains(z)) {
return true;
}
}
return false;
}

Another approach would be to use ArrayUtils.contains() method if you are using it in your project, as already shown here.

Finally, you can perform an O(n²) comparison with two for blocks to search for the desired value without the overhead of converting your array into a list, as stated above. However, I believe the approach shown in the code snippet above will be faster.

Trying to find a number in an array with an input and print if it is not equal to the input

Your code is comparing the user's input with just the first element of the array, i.e. array[0]. What you need to do is to loop through the whole array and compare the number with every element of the array.

How do I check if my int array is empty, with the standard value being 0?

You are probably using an int[]? The primitive type int can not be null. A very simple solution would be to use the wrapper class Integer.

Integer[] intArray = {null, 0, 10};

Most efficient way to check if an array contains a value in Java?

Your co-worker is incorrect when they assert that your method is creating a new data structure.

If you look at the API for Arrays.asList(), it says that it

Returns a fixed-size list backed by the specified array.

There is no reason to write your own code to iterate over the array when you can just wrap a List around it, and use its built-in methods.



Related Topics



Leave a reply



Submit