How to store different types of objects in ArrayList

前端 未结 5 2026
暗喜
暗喜 2021-01-25 22:20

I need to store different objects in the ArrayList. Objects are similar in nature but different and have different methods.

Circle c = new Circle();  
Cube s =          


        
5条回答
  •  北海茫月
    2021-01-25 23:24

    i think you can create a list of objects, and when use someone u cast to the specific type

    List obj = new ArrayList();
    
    if (obj.get(i) instanceof Class) {
        Class objCast = (Class)obj 
    }
    
    
    

    Objects DOC

    提交回复
    热议问题