0

I am getting this same error in eclipse! (watch desk)

I have problems with arrays. I always get this same error Expection in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at MyClass.main(MyClass.java:20) My code: public class MyClass { public static void main(String[] args) { int array[] = new int[2]; array[1] = 132; array[2] = 31; System.out.println(array[1] + array[2]); } }

18th Jan 2017, 2:13 PM
Velhokala
2 Antworten
+ 1
indexing in array starts from 0 so the first item is 0, second item 1 and so on array[0] = 132; array[1] = 31; System.out.println(array[0] + array[1]);
18th Jan 2017, 2:23 PM
Uran Kajtazaj
Uran Kajtazaj - avatar
0
Thank you soooper alot!!!!
18th Jan 2017, 2:23 PM
Velhokala