How to check if a Java array contains a specific value?
Hi everyone! I'm a beginner Java developer and I've encountered a problem: I need to check if a string array contains a specific value. For example, I have an array of user names:\n\n```java\nString[] users = {"Anna", "Ivan", "Maria", "Petr"};\n```\n\nAnd I want to check if the user "Ivan" is among them. I've tried several approaches, but they either don't work or look very cumbersome.\n\nFirst, I tried using the `contains()` method, like in collections:\n\n```java\nif (users.contains("Ivan")) { // Compilation error\n System.out.println("User found");\n}\n```\n\nBut it didn't work — the compiler complains that arrays don't have such a method.\n\nThen I wrote a `for` loop:\n\n```java\nboolean found = false;\nfor (int i = 0; i < users.length; i++) {\n if
No answers yet.
Log in to leave an answer