Check ArrayList for instance of object

前端 未结 5 1323
南方客
南方客 2021-01-02 11:57

I have a java method that should check through an ArrayList and check if it contains an instance of a given class. I need to pass the method the type of class to check for a

5条回答
  •  耶瑟儿~
    2021-01-02 12:34

    check one of the the item instance in the list

    if (object instanceof ArrayList) {
                            if (((ArrayList) object).get(0) instanceof ClassA) {
                                classAList= (ArrayList) object;
                            } else if (((ArrayList) object).get(0) instanceof ClassB) {
                                classBList= (ArrayList) object;
                            } else if (((ArrayList) object).get(0) instanceof ClassC) {
                                classCList= (ArrayList) object;
                            }
                        }
    

提交回复
热议问题