7. Iterating over an ArrayList


#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) );

}

}

What;s wrong with this code?


#2

closing parenthesis is missing here


#3

Should all of the temperatures be printed to the console when using the code:

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) );

#4

Yes it should.if it is not doing that for you then it's probably because you have an error in your code. You are missing closing braces for the class,the main and the for loop


#5

A post was split to a new topic: Why I'm still getting errors?


#6