Let’s say we have an ArrayList that stores items in a user’s online shopping cart. As the user navigates through the site and adds items, their cart grows bigger and bigger.

If we wanted to display the number of items in the cart, we could find the size of it using the size() method:

ArrayList<String> shoppingCart = new ArrayList<String>(); shoppingCart.add("Trench Coat"); System.out.println(shoppingCart.size()); // 1 is printed shoppingCart.add("Tweed Houndstooth Hat"); System.out.println(shoppingCart.size()); // 2 is printed shoppingCart.add("Magnifying Glass"); System.out.println(shoppingCart.size()); // 3 is printed

In dynamic objects like ArrayLists, it’s important to know how to access the amount of objects we have stored.



Detectives do a lot to solve a case. But who has more to do?

Print out the size of each detective’s to-do ArrayList:

  • sherlocksToDos for Sherlock Holmes
  • poirotsToDos for Hercule Poirot

So who has more to do? Print the name of the detective whose to-do list is longer. Was it Sherlock or Poirot?

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?