-1

I inserted these two ArraysList into this List:

    ArrayList<String> category = new ArrayList<>();
    ArrayList<Integer> number = new ArrayList<>();
    List list = new ArrayList<>();

    category.add("cat");
    category.add("dog");
    category.add("fish");
    category.add("Hamster");

    number.add(1);
    number.add(2);
    number.add(3);
    number.add(4);

    list.add(category);
    list.add(number);

Now how do I get the size of each array in this list?

MOHS3N
  • 229
  • 1
  • 4
  • 13
  • 3
    `"how do I get the size of each array in this list?"`: what kind of result are you expecting? – ernest_k Sep 26 '18 at 05:10
  • @ernest_k This list is then referred as a parameter to another `class` and does not have access to arrays `category` and `number` – MOHS3N Sep 26 '18 at 06:00

4 Answers4

5

Try like this

 int sizeOfCategory = category.size();
 int sizeOfNumber = number.size();

OR

    for(Object li : list) {
        System.out.println(((List) li).size());
    }
ArockiaRaj
  • 588
  • 4
  • 17
  • The second option will not work (actually it gives a compile-time error) with the code from the question since `list` is declared using the raw type `List` (see my [answer](https://stackoverflow.com/a/52511930/2838289) for more). – LuCio Sep 26 '18 at 07:59
  • The compile-time error is not present anymore. Now the compiler issues the warning: "References to generic type List should be parameterized". But this also applies to the accepted answer. I wonder why the answers here (given by users with >1k reputation) are using raw type since the [documentation](https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html) says: _you should avoid using raw types._. Doing one cast isn't even shorter than providing the generic type at declaration of `list`. – LuCio Sep 26 '18 at 10:55
1

Cast the result object of get method from the list with ArrayList.

for(int i=0; i<list.size(); i++) {
   System.out.println(((ArrayList)list.get(i)).size());
}
Shahid
  • 2,288
  • 1
  • 14
  • 24
0

Check object instanceOf, if it is type of ArrayList which you added, then down-cast it to ArrayList and get size.

list.add(category);
list.add(number);
for (Object o : list) {
    if (o instanceof ArrayList) {
        System.out.println(((ArrayList) o).size());
    } else {
         // unknown type
    }
}

If you want you can get List item type too.

System.out.println("Object is type of " + o.getClass());
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

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?

LuCio
  • 5,055
  • 2
  • 18
  • 34