-2

I have found a few questions similar to the problem I am facing, but I couldn't find solution. Example: Incompatible types List of List and ArrayList of ArrayList, Not able to understand how to define a List of List in java

The program should return list of lists. So, I declared a list of lists and then trying to add arraylists to it.

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

But, when I am trying to access each arraylist item from the list of lists as below, I get the error: incompatible types: List<Integer> cannot be converted to ArrayList<Integer>

for(ArrayList<Integer> subset:allsubsets)

When I try to convert the line to for(List<Integer> subset:allsubsets), it throws error that add, addAll don't exist for List type, which makes sense. Please help me understand how to access elements of list of lists in this case.

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;
}

Note: If I change the return type to arraylist of arraylists, it works for me. But, I want to make it work for the list of lists

zealouscoder
  • 86
  • 1
  • 3
  • 12

3 Answers3

4

Correct way to iterate your list of list should be:

    for(List<Integer> subset:allsubsets) { 

instead of:

    for(ArrayList<Integer> subset:allsubsets) { 

List<List<Integer>> allsubsets is declared as List of List, but the implementation is unknown.

Only you know the type of nested List is ArrayList, so either change foreach to use List<Integer> or manually cast your List<Integer> to ArrayList<> (this is not preferred)

One more thing:

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

This try to add a List of List (List<List<Integer>>) as element which should be List<Integer> hence compile error.

Change that statement to:

  allsubsets.addAll(moresubsets);
Mạnh Quyết Nguyễn
  • 15,590
  • 1
  • 17
  • 41
2

Let's try expanding that enhanced for loop into more basic components:

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.
}
StaticBeagle
  • 4,695
  • 2
  • 22
  • 30
0

I prefer to share with you the code I did for managing the same type of Object List you are trying to handle. Hope this helps.

    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;
}