简体   繁体   English

为什么我不能将 CustomType ArrayList 添加到 Object ArrayLists 的 ArrayList 中?

[英]Why I can't add CustomType ArrayList to ArrayList of Object ArrayLists?

I have Singleton class like this:我有 Singleton class 像这样:

public class Singleton
    private static Singleton instance;
    private ArrayList<Release> releases;
    private ArrayList<Place> places;
    ArrayList<ArrayList<Object>> list = new ArrayList<ArrayList<Object>>(2);

   private Singleton() {
       releases = new ArrayList<Release>();
       places = new ArrayList<Place>();
       list.add(release); //error, required AL<Object>, provided AL<Release> 
       list.add(places); //same
   }

   public static Singleton getInstance(){
    /* Singleton code */
   }

I thought that it is possible because, every Class extends Object class.我认为这是可能的,因为每个 Class 都扩展了 Object class。 My intention is to read from files where ALs are saved as object a then these ALs have in collection of one AL, where al.get(PLACES_INDEX) would return places and so on.我的意图是从 AL 保存为 object 的文件中读取,然后这些 AL 收集一个 AL,其中 al.get(PLACES_INDEX) 将返回位置等等。 It is a good approach or am I missing something?这是一个好方法还是我错过了什么?

Later on I would like to have some unified method, which would be something like:稍后我想有一些统一的方法,就像:

public ArrayList<T> getArrayList() {
   /*return places or releases based on <T>*/
}

I don't know if it's even possible since this class is Singleton.我不知道这是否可能,因为这个 class 是 Singleton。

I will explain why you're getting the error, but from what you describe this looks like a bad design for your class: don't store "generic" lists in a list, to access them based on a certain index.我将解释为什么您会收到错误,但从您的描述来看,这对于您的 class 来说似乎是一个糟糕的设计:不要将“通用”列表存储在列表中,以便根据某个索引访问它们。 And don't create a method like public ArrayList<T> getArrayList() { that returns one of the lists depending on the type T .并且不要创建像public ArrayList<T> getArrayList() {这样根据类型返回列表之一的方法T This is overengineering, and makes your code much harder to maintain, and easy to break.这是过度工程,使您的代码更难维护,也很容易被破坏。

Just keep the distinct lists separately, and provide getters for each one of them.只需分别保存不同的列表,并为每个列表提供 getter。 If you are reading from a file and want to deserialize the content into a data structure, simply create a class structure that models the content.如果您正在从文件中读取并希望将内容反序列化为数据结构,只需创建一个对内容建模的 class 结构。 Your code will be much simpler and easier to read.您的代码将更加简单易读。

Even though Release is a subclass of Object , ArrayList<Release> is not a subclass of ArrayList<Object> , therefore you cannot add an ArrayList<Release> to an ArrayList<ArrayList<Object>> (we say that generics are not covariant ).即使ReleaseObject的子类, ArrayList<Release>不是ArrayList<Object>的子类,因此您不能将ArrayList<Release>添加到ArrayList<ArrayList<Object>> (我们说generics 不是协变) . If Java allowed you to do that, then you can end up with a scenario that breaks generic usage of the code:如果 Java 允许您这样做,那么您最终可能会遇到破坏代码通用用法的场景:

ArrayList<ArrayList<Object>> list = new ArrayList<>();
ArrayList<Release> releases = new ArrayList<>();
list.add(releases);   // imagine this is allowed
ArrayList<Object> releasesFromList = list.get(0);
releasesFromList.add(place); // oops, added a place to list of release

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

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