Bug or am I going mad?


#1



ok so according to instructions, im supposed to print out the value of 89, which is index 2, correct?
the "Get code" button gave the code below, its the same as mine exept that it prints out index 3. isnt index 3 the value 94? I believe that array index starts at 0, not 1. im not sure if this is a bug, or im just plain dumb.


import java.util.ArrayList;

public class TemperaturesB {
	
	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);
    System.out.println( weeklyTemperatures.get(3) );


	}
}


#2

the .add(); function overwrites the specified index, right?


#3

No, it inserts an element at that index, and thus shift's 89 to the next one, hence index 3.


#4

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