How to store different types of objects in ArrayList

前端 未结 5 2078
暗喜
暗喜 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:12

    List list = new ArrayList<>();
    
    list.add(new ArrayList<>());
    list.add(Integer.valueOf("24"));
    list.add(Long.valueOf("25"));
    list.add("STRING");
    list.add(new Object());
    
    System.out.println(Arrays.toString(list.toArray()));
    //Console print: " [[], 24, 25, STRING, java.lang.Object@4554617c] "
    
    
    

    You may store any object type.

    提交回复
    热议问题