+ 2

Why my this code shows 'NO OUTPUT' ?

import java.util.ArrayList; public class MyClass { public static void main(String[ ] args) { int i; ArrayList<Integer> colors = new ArrayList<Integer >(10); for(i=0;i<colors.size();i++) { colors.add(i); } System.out.println(colors.get(1)); } }

5th May 2017, 4:27 PM
Faham Khan
Faham Khan - avatar
1 Respuesta
+ 11
colors.size() returns 0 at first. It doesn't matter how much (10 here) you specify in declaration. Change the loop condition to "i < 10" for your code to work.
5th May 2017, 4:56 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar