Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Check if item is in array [duplicate]

Tags:

java

arrays

Possible Duplicate:
In Java, how can I test if an Array contains a certain value?

I have an array setup as follows:

Material[] blockedlevel1 = {
            Material.mymaterialone, Material.mymaterialtwo  
        };

How do I see if a Material is in this array?

like image 218
DannyF247 Avatar asked Dec 17 '22 02:12

DannyF247


1 Answers

How about looking for it in the array?

for (Material m : blockedlevel1) {
    if (m.equals(searchedMaterial)) { // assuming that equals() was overriden
        // found it! do something with it
        break;
    }
}
like image 116
Óscar López Avatar answered Dec 18 '22 16:12

Óscar López