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


#1




import java.util.ArrayList;

public class TemperaturesC {
	
	public static void main(String[] args) {

		ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>() {
		weeklyTemperatures.add(78);
		weeklyTemperatures.add(67);
		weeklyTemperatures.add(89);
		weeklyTemperatures.add(94);
		weeklyTemperatures.add(2, 111);
    }
    
		for ( int j = 0; j < weeklyTemperatures.size(); j++) {
    System.out.println(weeklyTemperatures.get(j) );
    }
  }
}


#2

you just need a ; after

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

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


#3

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


#4

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.


#5

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?


#6

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


#7

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


#8

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