0

i have an array list of arraylists of object type trying to get the size of a specific array list but i keep getting the size of the the whole arraylist

healthyKoalas = new ArrayList<Koala>();
hKoalas = new ArrayList<ArrayList<Koala>>();

public void hkoala(int x)
    {
         for (int i=0; i<x; i++) // x is the number of healthy koalas in an observation point
         {
             int age = (int)(Math.random()*18)+1; 
             Koala k = new Koala(age); 
             addHKoala(k);
             healthyKoalas.sort(Comparator.comparing(Koala::getAge).reversed());
             //Collections.sort(healthyKoalas, Collections.reverseOrder());
            }
        hKoalas.add(healthyKoalas);
    }

then when i try

hKoalas.get(x).size();

it gives me the number of all the koala objects while i want to get the size of a specific inner arraylist

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43

1 Answers1

1

Given hKoalas = new ArrayList<ArrayList<Koala>>();, then hKoalas.get(1).size() will give you the number of elements of the second 'inner list' (index 1 = the second one in the store, as index 0 is the first one). If you think it is returning the count of ALL koalas across all the inner lists, you've got another bug in your code and you have misidentified what's happening.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72