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 out in to multiple lists of less that size numValuesInClause.
I'm having difficulty with this code, with various conversions/casting between ArrayList<Integer> and List<Integer>
For example List.subList(x,y) returns a List<E>
What's the best way to work here?
The current code shown here is what makes the most sense to me, but has the compilation error shown.
Use:
List<List<Integer>> newList = new ArrayList<List<Integer>>();
instead of:
List<List<Integer>> newList = new ArrayList<ArrayList<Integer>>();
The reason for this, is that you are instantiating a concrete ArrayList of a generic element List<Integer>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With