FAQ: Learn Java: ArrayLists - Review

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

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!

Hello, I just wanted to check if anyone else has problems with learning environment disconnecting all the time.

Can anyone help?

Take a look at this article and see if it’s of any help.

1 Like

I cannot seem to get my practice code to run. I have multiple print statements in main, but when I type java List in the terminal, I get nothing. Am I missing something?

1 Like

Welcome to the forums!

Did you make sure to first compile the .java file, then execute the compiled code (all using the right commands)?

The exercise on compilation and execution can be found here.

How do I replace that terminal on the right, which doesn’t work, with an IntelliJ IDEA window? I can’t test the code. javac List.class returns an error:

javac: invalid flag: List.class
Usage: javac
use --help for a list of possible options

If I just type java List, nothing happens.

try
javac List.java
to compile
and, afterward,
java List to run.

Oh, my bad, I though you compile class-files into java-files, not the other way around. Thanks!

Why do I have to create arrayLists inside main methods? You usually declare and initialize things before main methods (for example, constructor methods typically precede them). However, if I create an arrayList before my main method, the code doesn’t work. Why?

UPD: I discovered it has to do with static/non-static qualities (though, if you feel like explaining it to me in more detail, I would appreciate it as I don’t fully get it!). Unfortunately, I forgot what static/non-static means. To add insult to injury, I don’t remember which previous lesson mentioned it, and it’s very inconvenient to search for information in a course on Codecademy! You can’t even preview “sub-lessons” of each lesson. Basically, you have to do your search manually. It’s a pitty!

UPD2: It would be nice if Codecademy stressed the fact that arrays must be created inside main methods. Could you consider including that information in the lessons? I may be not the last person who is confused by that

1 Like

I hope someone replies to this

Hey guys, Idk why my code is not running and shows this error below. Thanks!

import java.util.ArrayList;

class List {
  
  public static void main(String[] args) {
    // {"Holmes", "Poirot", "Marple", "Spade", "Fletcher"};
    ArrayList<String> detectives = new ArrayList<String>();
    detectives.add("Holmes");
    detectives.add("Poirot");
    detectives.add("Marple");
    detectives.add("Spade");
    detectives.add("Fletcher");
    System.out.println(detectives.indexOf("Fletcher"));
  }
    System.out.println(detectives.size());
    System.out.println(detectives.get(1));
}

// You wrote:
class List {
  public static void main(String[] args) {
    ...
    System.out.println(detectives.indexOf("Fletcher"));
  }
    System.out.println(detectives.size());
    System.out.println(detectives.get(1));
}

// It should be:
class List {
  public static void main(String[] args) {
    ...
    System.out.println(detectives.indexOf("Fletcher"));
    System.out.println(detectives.size());
    System.out.println(detectives.get(1));
  }
}

Thank you! I got this.