Monday, 13 February 2017

If x in array in Java




In python, you can use a very simple if statement to see if what the user has entered (or just a variable) is in a list:




myList = ["x", "y", "z"]
myVar = "x"

if myVar in x:
print("your variable is in the list.")


How would I be able to do this in Java?


Answer



If your array type is a reference type, you can convert it to a List with Arrays.asList(T...) and check if it contains the element




if (Arrays.asList(array).contains("whatever"))
// do your thing

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...