Difficulty working with subList when using a list of lists

public List<List<Integer>> splitList(
        List<Integer> values) {


    List<List<Integer>> newList = new ArrayList<ArrayList<Integer>>();
    //Type mismatch: cannot convert from ArrayList<ArrayList<Integer>> to List<List<Integer>>

    while (values.size() > numValuesInClause) {
        List<Integer> sublist = values.subList(0,numValuesInClause);
        List<Integer> values2 = values.subList(numValuesInClause, values.size());   
        values = values2; 

        newList.add( sublist);
    }
    return newList;
}

      

I want to pass in a list of integers and split it into multiple smaller lists numValuesInClause

.

I am having difficulties with this code, with various conversions / ebb between ArrayList<Integer>

andList<Integer>

For example List.subList(x,y)

returns aList<E>

What's the best way to work here?

The current code shown here is the most important to me, but it has a compilation error.

+3


source to share


1 answer


Using:

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

      

instead:



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

      

The reason for this is that you are creating a specific ArrayList

generic itemList<Integer>>

+3


source







All Articles