+ 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)); } }
1 Answer
+ 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.