FAQ: Learn Java: ArrayLists - Adding an Item

This community-built FAQ covers the “Adding an Item” 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 Adding an Item

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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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!

import java.util.ArrayList;

class ToDos {
    
  public static void main(String[] args) {
    
    ArrayList<String> toDoList = new ArrayList<String>();
    String toDo1 = "Water plants";
    // Add more to-dos here:
    String toDo2 = "Clean gutters";
    String toDo3 = "Grocery shopping";
    
    // Add to-dos to toDoList
    toDoList.add(toDo1);
    toDoList.add(toDo2);
    toDoList.add(toDo3);
    
    System.out.println(toDoList);
      
    
  }
  
}

my solution for step 3/9… You have to choose/create the “tasks” strings on your own. These two are things I have to do soon, so I chose those for my program.

when we print arrayList why it not printing out the location of its memory like the way it did with Array?

7 Likes

I didn’t understand this part. Could you explain please? What are those “some methods”? What “fancy casting”?

In this case, the items stored in this ArrayList will be considered Objects . As a result, they won’t have access to some of their methods without doing some fancy casting.

Why are we declaring String s in this code? We have an option to use add method directly right?. Can someone help me understand.

import java.util.ArrayList;

class ToDos {

public static void main(String args) {

ArrayList<String> toDoList = new ArrayList<String>();
String toDo1 = "Water plants";
// Add more to-dos here:
String toDo2 = "Learning Java";
String toDo3 = "Reading Books";
 // Add to-dos to toDoList
toDoList.add(toDo1);
toDoList.add(toDo2);
toDoList.add(toDo3);

System.out.println(toDoList);

}

}

Correct me if I’m wrong:
For ArrayList, we specifically no need to declare the length as we do in Arrays so we can declare as many as elements but why does it sentence tells that we cannot insert a value at an index that doesn’t exist?

“Also, note that an error will occur if we try to insert a value at an index that does not exist.”

1 Like

I would assume this is because otherwise you may try to insert something at position 8 although your array has only 5 fields…

Hello,
Any insight about this:

“when we print arrayList why it not printing out the location of its memory like the way it did with Array?”

Thanks !

In regards to ArrayLists:
I noticed that when printing the arraylist it didn’t print out the memory address but instead printed out the Strings we initialized. Is this just due to the ArrayList class having some code in the background that already converts Strings ToString? Just academically curious about what’s going on in the background.

Thank you!

Update: Found an answer here. Looks like it overrides ToString in the class itself:

The main difference between an array and an arraylist is that an arraylist is a class that is written in Java and has its own implementation (including the decision to override toString) whereas arrays are part of the language specification itself. In particular, the JLS 10.7 states:

The members of an array type are all of the following:

  • The public final field length
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions.
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

In other words the language specification prevents the toString method of an array to be overriden and it therefore uses the default implementation defined in Object which prints the class name and hashcode.

Why this decision has been made is a question that should probably be asked to the designers of the language…