Question about for loop. 7/13


#1

https://www.codecademy.com/courses/learn-java/lessons/data-structures/exercises/arraylist-iterating?action=lesson_resume&link_content_target=interstitial_lesson

So, the code came back correctly. But I have a question about it. Where did the ‘j’ in the for loop came from? I don’t get that part.



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

Well, you defined it - first you declared that initial value of j is 0, than you told us that j must be less than weeklyTemperatures.size, and finally you declared that for loop should add 1 in each iteration.


#3

It’s just what you named the temporary variable held inside your loop.
You’ve probably seen it as i or counter, etc etc
It could be named anything. The important part is understanding HOW your for loop does what it does.


#4

Thanks! It’s the int j = 0 right? So I could name it anything else?


#5

Yes - it could be i, k,z - whatever you want. :slight_smile:


#6

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