Possible duplicate:In Java, how can I check if an Array contains a specific value?
I have an array setup as follows:
Material[] blockedlevel1 = { Material.mymaterialone, Material.mymaterialtwo };
How to find out if the material is in this array?
How to find it in an array?
for (Material m : blockedlevel1) { if (m.equals(searchedMaterial)) { // assuming that equals() was overriden // found it! do something with it break; } }
If you need an easy way to check if an element is part of a collection, you should probably consider another data structure such as Set (and use contains ()). With an array, you can only iterate over elements and compare them.
How about searching with the Arrays class?
See Arrays # binary search
Or as someone suggested, turn your array into List and use contains () . Remember that you may need to override the Material # equals method.
Source: https://habr.com/ru/post/1401506/More articles:Trying to use reduce () and lambda with a list containing strings - pythonCan reading / writing the NETLINK jack never work? - cNewbie Django: creating a project with multiple applications, or all in one - pythonfading slide show in image. xCode 4.2 - xcodeSystem.InvalidOperationException: Matching information and metadata not found for EntityType - asp.netStrange JSON.parse () error in node.js - jsonDownloading huge files using PHP or any other language? - javaWindows installation / installation project: forced installation in "program files", not x86? - windows-installerMongodb.lock permission prohibited - mongodbWhy do I need to lock the lock? - syntaxAll Articles