1

Don't want to use loops. I tried the below code but it doesn't work for primitive types.

if ( Arrays.asList( myMatrix ).contains( -1 ) )
{
    // do something
}

What shall be done here?

2
  • 2
    You need a loop to do this, and the example you posted demonstrates lazy, inefficient coding Commented Dec 6, 2015 at 1:09
  • 2
    @ControlAltDel: I agree with that, but in Ezazel's defense, (1) a lot of people advocate using stuff like the Streams API these days , (2) there really should be a contains method on java.util.Arrays, and (3) that some stuff does not work with int, but works with Integer is a bit lame. Commented Dec 6, 2015 at 1:18

3 Answers 3

3

You can't avoid iteration in your code since we need some way to test all elements in your array (at least until we will find the one we are looking for). But to make your life easier create some additional utility method like (name could be probably better/more descriptive) public static boolean contains(int[] array, int element) which will handle iteration for us. Then simply use it like if(contains(matrix, -1)).


In Java 8 you could use IntStream which handles int[] myMatrix array correctly (like Array.asList handles Integer[] myMatrix).

So as condition you could use something like:

IntStream.of(myMatrix).anyMatch(i -> i == -1)
Sign up to request clarification or add additional context in comments.

2 Comments

I upvoted, because that is what the question asked for. But I would not do that in this case. Cannot be faster, arguably harder to read.
@Thilo Agreed. I only posted it as example of how code can look like. But I am surprised that Arrays doesn't have method which handles this task already. EDIT: based on your answer I see we agree on that.
3

Java should really have a method contains on java.util.Arrays.

Commons Lang has it, for example.

So I would either find it in the libraries I already use, or make a static helper method myself (with a simple loop inside).

Comments

2

"I don't want to use loops" -- you can't always get what you want, and in fact in this situation, use a loop.

8 Comments

You could hide that loop behind contains, but there is still a loop in there.
You cannot directly use contains on an array of primitives. You have to jump through some hoops (and potential inefficiencies). But that's more of a failing of Java than the programmer's in my book.
@Thilo: thanks. And 1+ to Pshemo's answer, although if I had the energy, I'd look through the source code generated by it.
@Ashiquzzaman: to be honest, I didn't much like the question, or my answer.
I don't like my answer either. Or the other one. Even though all three are correct. Sad situation all around. This kind of stuff makes people roll their eyes at Java.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.