The problem is that you are using the raw typ of List
. You would get it on your own, if you had declared list
using a generic type T
for List<T>
which provides a size()
method. Using the raw type the interface List
will return elements of type Object
- hence there is no size()
method.
As list
contains two instances of ArrayList
there are some candidates for T
available. For example:
ArrayList<?>
List<?>
Collection<?>
It depends on what you need to do with the inner lists (category
, number
). If you need only the size of them then Collectiion<?>
will be sufficient.
System.out.println(list.get(0).size()); // 4
System.out.println(list.get(1).size()); // 4
Since the inner lists are using Integer
and String
as their types, there is no much they have in common in order to replace the wildcard <?>
. String
and Integer
are both implementing Comparable
but are only comparable to instance of their own type. Thus declaring List<Collection<? extends Comparable<?>>> list
will not give any additional value. Hence having list
which contains both inner lists of different types without a useful common type limits the usability of list
to cases where you don't need the concrete type of the elements of the inner lists.
Further reading: What is a raw type and why shouldn't we use it?