简体   繁体   English

将随机类型的arrayList添加到arrayList

[英]Adding an arrayList of random type to an arrayList

So I have looked a lot online on how to do this and what I get is this 所以我在网上看了很多关于如何做到这一点的,我得到的是

ArrayList<ArrayList<Type>> arrayLists = new ArrayList<ArrayList<Type>>();
ArrayList<TypeA> typeA = new ArrayList<TypeA>();
ArrayList<TypeB> typeB = new ArrayList<TypeB>();
...

Assuming that I have a class called TypeA, and a class called TypeB, and the array list typeA would contain multiple objects of the class TypeA. 假设我有一个叫做TypeA的类和一个叫做TypeB的类,并且数组列表typeA将包含TypeA类的多个对象。 The same goes for typeB B型也一样

Now my question what do I put where Type is for the arrayList, and how do I add an arrayList to arrayLists? 现在我的问题是,将arrayList的Type放在哪里,如何将arrayList添加到arrayLists? If I did this I would get an error: 如果这样做,我会得到一个错误:

arrayLists.add(typeA);

I am trying to make a list that contains arrayLists, if my approach is wrong please tell me. 我正在尝试制作一个包含arrayLists的列表,如果我的方法有误,请告诉我。

If you want an ArrayList which contains ArrayLists with unknown contents, use the type ArrayList<ArrayList<?>> for the outer list. 如果要使用包含内容未知的ArrayList<ArrayList<?>> ,请对外部列表使用ArrayList<ArrayList<?>>类型。

This code should compile with no problems: 这段代码应该没有问题地编译:

ArrayList<ArrayList<?>> arrayLists = new ArrayList<ArrayList<?>>();
ArrayList<TypeA> typeA = new ArrayList<TypeA>();
ArrayList<TypeB> typeB = new ArrayList<TypeB>();

arrayLists.add(typeA);
arrayLists.add(typeB);

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

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