ArrayList<Integer> marks = new ArrayList<Integer>(3);
marks.add(55);
marks.add(54);
marks.add(60);
System.out.println(marks.get(2)); //60
We will get error message if input position is beyond the last element of the ArrayList.
Using size() we can get the number of elements present in an ArrayList.
ArrayList<Integer> marks = new ArrayList<Integer>(3);
marks.add(55);
marks.add(54);
marks.add(60);
for(int i=0; i < marks.size(); i++)
System.out.println(marks.get(i));