FAQ: Learn Java: ArrayLists - ArrayList Size

This community-built FAQ covers the “ArrayList Size” exercise from the lesson “Learn Java: ArrayLists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise ArrayList Size

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi Guys,
Actually I understood for the task: “So who has more to do? Print the name of the detective whose to-do list is longer. Was it Sherlock or Poirot?”

… that we should implement code that checks which ArrayList is bigger and then print it.
I would really like to know how to do that.

And I don’t mean by using a simple
if (“Sherlock > Poirot”) {
“print Sherlock”
} else {
“print Poirot”

I mean code that works for any amount of input ArrayLists.

Thanks in Advance

1 Like

Hello, @davidgiuliano8613870.

Welcome to the forums.

You’re getting a little ahead of the tutorial, but something like this may be what you’re inquiring about.

    if(sherlocksToDos.size() > poirotsToDos.size()) {
    } else if(sherlocksToDos.size() < poirotsToDos.size()) {
    } else {
      System.out.println("Both Sherlock and Poirot have the same number of things to do.");

Happy coding!

import java.util.ArrayList;

class ToDos {
  public static void main(String[] args) {
    // Sherlock
    ArrayList<String> sherlocksToDos = new ArrayList<String>();
    sherlocksToDos.add("visit the crime scene");
    sherlocksToDos.add("play violin");
    sherlocksToDos.add("interview suspects");
    sherlocksToDos.add("solve the case");
    sherlocksToDos.add("apprehend the criminal");
    // Poirot
    ArrayList<String> poirotsToDos = new ArrayList<String>();
    poirotsToDos.add("visit the crime scene");
    poirotsToDos.add("interview suspects");
    poirotsToDos.add("let the little grey cells do their work");
    poirotsToDos.add("trim mustache");
    poirotsToDos.add("call all suspects together");
    poirotsToDos.add("reveal the truth of the crime");
    // Print the size of each ArrayList below:
    // Print the name of the detective with the larger to-do list:

My solution for exercise 4/9, both question’s solutions can be found in this code

Hello. My question is… Why do we use the size() method for ArrayLists instead of the .length property for Arrays? Any help would be appreciated.

1 Like

What even is the point in doing this? We have gone from getting the size of arrays back to a “Hello World!” where we look at two values and write to print this line. What should I take from this?

Hello @janb1!

We should use the .size() method to compare the two ArrayLists and output the larger ArrayList.

In Python,
Arrays are tupels
ArrayList are lists.
Since we can’t change the elements of a Array.
Am I right?

In ArrayLists we use Size() . In this method we will never have to use parameters right? Eventhough there are paranthesis.

This is incorrect. Firstly, there are many differences between arrays and tuples, ArrayLists and lists, and their functionalities, meaning they cannot be directly compared as equivalent to one another.

Here are a few points on the four concepts.

Python Tuples

  • Elements and length are immutable (cannot be changed).
  • Can store objects of multiple data types in the same tuple.

Python Lists

  • Elements and length are mutable (can be changed).
  • Can store objects of multiple data types in the same list.

Java Arrays

  • Elements are mutable.
  • Length is immutable.
  • Can only store objects of one data type in the same array.

Java ArrayLists

  • Elements and length are mutable.
  • Can only store objects of one data type (though you can use a generic data type to store multiple data types).


Python also has an array module that behaves similarly to a Java array.

1 Like

Yes, no parameters should be passed to this method. The parentheses indicate that we are calling an instance method rather than accessing an instance variable. See the documentation here.

1 Like

Thanks for the reply <3

1 Like

Eventhough we don’t need toString method to print out a Arraylist. Why did they use it on this exercise

I had to do some research to find out exactly why this is the case, so I encourage others to jump in and add to my reply.

Here’s some vital background to know.

The ArrayList class (from which ArrayList objects are created) doesn’t have its own toString() method. Rather, it overrides the toString() method from the Object class. The Object.toString() method returns a string representation of the object, consisting of the class of the object, an @ sign, and a hex code. See the documentation for Object.toString() here.

This method is overridden by the ArrayList class to print (as it seems to me) an opening square bracket, followed by the contents of the ArrayList separated by commas and spaces, followed by a closing square bracket. See an article on the overridden method here.

Now onto why toString() is used in the exercise.

You’re correct in that it’s not necessary to print out the ArrayList. The toString() of a class is automatically called when we use System.out.println() to print out an object of that class. You can learn more about this here.

Because Object.toString() is overridden by the ArrayList class to print out the contents of the ArrayList object, using the toString() method is not necessary to print out the ArrayList.

Other classes (like String, Integer, and Boolean) have their own toString() methods that return the respective string representations of their objects. Or, let’s say you create your own class without a extending from a superclass and without overriding the Object.toString() method (since all classes are implicitly subclasses of the Object class in this case). This class would inherit the toString() method from the Object class, meaning it would print something like Pizza@1fee6fc (the hex code, and likely the class, will be different).

Now here’s why toString() can be useful.

You yourself can override the toString() method of any class to print out the object and/or data in a specific format.

public class Pizza {
  private int size;

  public Pizza(int s) {
    size = s;

  public String toString() {
    return "Pizza[" + size + " in]";

In this case, if I created a Pizza with size 12 and printed the object, I would get the custom string representation of Pizza[12 in] rather than Pizza@[hexcode] where hexcode is the hex code.