FAQ: Learn Java: Loops - For-Each Loops

This community-built FAQ covers the “For-Each Loops” exercise from the lesson “Learn Java: Loops”.

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

Learn Java

FAQs on the exercise For-Each Loops

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!

import java.util.ArrayList;

class MostExpensive {
  
  public static void main(String[] args) {
    
    ArrayList<Double> expenses = new ArrayList<Double>();
    expenses.add(74.46);
    expenses.add(63.99);
    expenses.add(10.57);
    expenses.add(81.37);
    
    double mostExpensive = 0;
    
    // Iterate over expenses
    for (double expense : expenses) {
      if (expense > mostExpensive) {
        mostExpensive = expense; 
      }
    } // end of for-each loops
    
    System.out.println(mostExpensive);
    
  }
  
}

I understand that is a convention to use the singular form (expense) of the plural form (expenses) when creating for-each loops… but how does the program know that “expense” is referring to the individual elements/items of the Array/ArrayList “expenses”?

5 Likes

It’s very weird that they do not accept simpler solutions for this exercise.
The notation posted above is required.

for (double expense : expenses) {
  if (expense > mostExpensive) mostExpensive = expense;
}

(A solution omitting the curly brackets gets rejected)

for (double expense : expenses) {
  mostExpensive = expense > mostExpensive ? expense : mostExpensive;
}

(A solution using the ternary operator, although we haven’t learned this, is also rejected)

I also had this question. Also how does expense > mostExpensive result in the highest value in the arrayList

if (expense > mostExpensive) {mostExpensive = expense);
if expense is greater than mostExpensive, the value of mostExpensive is updated and reassigned to expense which results in the highest value!

3 Likes

Thank you for your answer. I had the same question.

1 Like

I understand the functions of all the operators in these lines of code, and can follow the logic. However, I can do this because I know expense is the singular of expenses, and I know the coder wants the highest value double in the expenses list because I understand English. Still, how does the java compiler know
expense = individual elements of the ArrayList expenses;
mostExpensive = element with the highest valued double;
Could we have used any variable names (i.e. items, priciestItem) and it would have worked out the same because of the logic of the lines? Lol ugh, I’m really confused.

1 Like

So my Girl just explained it to me.
The word “expense” isjust a self chosen variable name. Important is the right value “double”.
As far as I understand every time the code hits a double in the expenses list the code runs through and checks if the new double is bigger than mostExpensive. If it is bigger than mostExpensive it is assigned to mostExpensive.
When it is through with the list it will have found the “priciestItem”.