7/13 Please help me figure out why array does not print to screen


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>


import java.util.ArrayList;

public class TemperaturesC {

public static void main(String[] args) {

	ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>() {
	weeklyTemperatures.add(2, 111);

	for ( int j = 0; j < weeklyTemperatures.size(); j++) {
System.out.println(weeklyTemperatures.get(j) );


<do not remove the three backticks above>

you just need a ; after

ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>() // here

and remove opening( { ) and closing ( } ) curly braces

Thanks; it now works. So now I will remember that the creation of a new ArrayList is a statement and needs a ;

It’s not! new ArrayList<Integer>() is an expression. Also, not all statements end in semi-colon.

Assignment and variable declaration are both statements ending with semi-colon though.

Thanks. I am determined to get this down.

From what I read on aboutTech: A statement does something. An expression is a statement that has value. Expressions must always be inside brackets.

Where is the value in the expression? Created ArrayList( ) , Created ArrayList with values set to 0, right? So the value that the new ArrayList has is a null value or 0 value? I suppose even a 0 or null value is still considered a value. Are all statements using a method ( ) considered expressions?

An expression isn’t necessarily a statement, but there is a kind of statement that is only an expression, for example a function call, typically one with a side-effect which as you say, does something.

Expressions do not need to be in brackets of any kind, no. A counter-example:

int x = 5;

5 is an expression, there are no brackets there. Although, yes, when talking about java specifically it is at least going to be inside a class and perhaps a method or something as well but that woud be saying more than I know

new ArrayList<Integer>() is an expression, and the value that it evaluates to is an instance of ArrayList

Thanks. Now I know, creating a new ArrayList is an expression.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.