关于java:错误:不兼容的类型:List 无法转换为ArrayList

error: incompatible types: List cannot be converted to ArrayList

我发现了一些与我面临的问题类似的问题,但我找不到解决方案。
示例:不兼容的类型ArrayList的列表和ArrayList,无法理解如何在java中定义List列表

该程序应返回列表列表。 所以,我宣布了一个列表列表,然后尝试添加arraylists。

1
allsubsets = new ArrayList<List<Integer>>();

但是,当我尝试从列表列表中访问每个arraylist项目时,我得到错误:incompatible types: List cannot be converted to ArrayList

1
for(ArrayList<Integer> subset:allsubsets)

当我尝试将行转换为for(List subset:allsubsets)时,它会抛出添加的错误,对于List类型不存在addAll,这是有道理的。 在这种情况下,请帮助我了解如何访问列表列表的元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public List<List<Integer>> subsets(int[] nums) {
    List<Integer> arrayList = new ArrayList<Integer>();
    for(int i:nums) {
        arrayList.add(i);
    }
    return subsets(arrayList,nums.length);
}

public List<List<Integer>> subsets(List<Integer> arrayList, int index) {
    List<List<Integer>> allsubsets;
    if(index == -1) {
         allsubsets = new ArrayList<List<Integer>>();
         allsubsets.add(new ArrayList<Integer>());
    }
    else {
        allsubsets = subsets(arrayList, index-1);
        int item = arrayList.get(index);
        List<List<Integer>> moresubsets =  new ArrayList<List<Integer>>();
        for(ArrayList<Integer> subset:allsubsets) {
        //The line above throws error as I created list of lists

            List<Integer> newsubset = new ArrayList<Integer>(); //create new subset
            newsubset.addAll(subset); // add all old items
            newsubset.add(item); // add new item
            moresubsets.add(newsubset); //add subset to moresubsets
        }
        allsubsets.add(moresubsets); // add to actual one
    }
  return allsubsets;
}

注意:如果我将返回类型更改为arraylists的arraylist,它对我有用。 但是,我想让它适用于列表列表


迭代列表列表的正确方法应该是:

1
    for(List<Integer> subset:allsubsets) {

代替:

1
    for(ArrayList<Integer> subset:allsubsets) {

List> allsubsets被声明为List of List,但实现未知。

只有您知道嵌套List的类型是ArrayList,因此要么将foreach更改为使用List,要么手动将List转换为ArrayList<>(这不是首选)

还有一件事:

1
    allsubsets.add(moresubsets); // add to actual one

这尝试添加List of List(List>)作为元素,应该是List因此编译错误。

将该声明更改为:

1
  allsubsets.addAll(moresubsets);


让我们尝试将增强的for循环扩展为更基本的组件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(ArrayList<Integer> subset:allsubsets) {
    //The line above throws error as I created list of lists
}

// this is roughly equivalent to
Iterator<List<Integer>> it = allsubsets.iterator();
while(it.hasNext()) {
    ArrayList<Integer> subset = it.next(); // Error
    // Since the iterator was defined as an iterator to a List<List<Integer>>,
    // it.next() will return the next element in allsubsets
    // which happens to be an List<Integers>.
    // You can't assign a reference of a parent type to a child. However
    // the opposite is perfectly fine, assigning a reference of a child type
    // to a parent.

    // If we change subset to be a List<Integer> i.e.
    // for(List<Integer> subset : allsubsets)
    // then we are assigning a reference of a List<Integer> to a List<Integer>
    // so no problem.
}


我更愿意与您分享我为管理您尝试处理的相同类型的对象列表所做的代码。 希望这可以帮助。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    public static void main(String[] args) {

    List<List<Integer>> allsubsets = setSubsets();

    List<List<Integer>> allsubsets2 = new ArrayList<>();

    allsubsets2.addAll(allsubsets);
    int i= 0;
    for (List<Integer> test : allsubsets2) {
        System.out.println(i +" Lista");
        for (Integer integer : test) {
            System.out.println(integer);
        }
        i++;
    }
}
public static List<List<Integer>> setSubsets() {
    List<List<Integer>> allsubsets = new ArrayList<List<Integer>>();
    List<Integer> listInteger1 = new ArrayList<>();
    List<Integer> listInteger2 = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        listInteger1.add(i);
    }
    for (int i = 1010; i < 1110; i++) {
        listInteger2.add(i);
    }
    allsubsets.add(listInteger1);
    allsubsets.add(listInteger2);
    return allsubsets;
}