简体   繁体   中英

How to check if a object is a type of enum in arraylist in java?

I have a simple question. Say I have an arrayList that will hold three different types from my enum below. How do I check to see if one of my items in my arraylist is a specific enum in java? For instance, how do I check to see is a object is a small through a if statement? I'm pretty new to using arraylists so I am not to sure what I can and can't do with them.

// My ArrayList...

ArrayList<sizes> myItems = new ArrayList<sizes>; 

// My enum...

     enum itemType { 

          SMALL, MEDIUM, LARGE }
}

For example if you want to check if first item is large:

if(myItems.get(0)==itemType.LARGE)
    System.out.println("true");

A more general solution:

for (int i=0;i<myItems.size();i++) 
    if(myItems.get(i)==itemType.SMALL)
        System.out.println("item at index: "+i+" is small");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM