How to store multiple datatypes in an array?

后端 未结 3 1626
一向
一向 2021-01-02 05:48

I\'m looking for something like an Array, but it needs to store multiple data types. The Oracle Java tutorials says, \"An array is a container object that holds a fixed numb

3条回答
  •  悲&欢浪女
    2021-01-02 06:29

    You should consider the use of the typesafe heterogeneous container pattern.

    There the data is stored in a Map, Object> and access to the map is hidden behind generic methods, that automatically cast the return value.

    public  T getObjectByKey(Key key)
      return (T) map.get(key);
    

    The same for put.

提交回复
热议问题