简体   繁体   English

如何检查 object 是否是 java 中 arraylist 中的枚举类型?

[英]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.假设我有一个 arrayList 将包含下面我的枚举中的三种不同类型。 How do I check to see if one of my items in my arraylist is a specific enum in java?如何检查我的 arraylist 中的一个项目是否是 java 中的特定枚举? For instance, how do I check to see is a object is a small through a if statement?例如,我如何通过 if 语句检查 object 是否为小? 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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