please explain me the flow of this prog
import java.util.*; public class listinterface { /** * @param args */ public static void main(String as[]) { ArrayList al= new ArrayList(); al.add(new Integer(99)); al.add("sri"); al.add("123"); al.add("sri@jlc"); al.add("sri"); System.out.println(al); al.add(0,"aaaa"); al.add(2,"bbbb"); al.remove(4); System.out.println(al); System.out.println(al.get(2)); System.out.println(al.indexOf("Sri")); System.out.println(al.lastIndexOf("sri")); al.set(0,"jlc"); System.out.println(al); List list=al.subList(1,4); System.out.println(list); System.out.println("forward order"); ListIterator li= al.listIterator(); while(li.hasNext()){ System.out.println(li.next()); } System.out.println("reverse order"); while(li.hasPrevious()){ System.out.println(li.previous()); } } }
Thanks
In the given code, an object of ArrayList is created. The method add() then adds an integer value and four string values to the array list. And then System.out.println(al) displays the array list values. The method al.add(0,"aaaa") adds an element "aaaa" at the postion 0 to the list and the al.add(2,"bbbb") adds the element "bbbb" at the position 2 to the list. The method remove(4) removes the element specified at position 4 from the list. The method get(2) displays the element of the list whose position is 2. As there is no element "Sri" so the method indexOf("Sri") returns -1. The method lastIndexOf("Sri") returns the last occurring index of the element from the list. The method set(0,"jlc") replaces the element specified at the postion 0 with the "jlc". The ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction. The method next() returns the next element in the list or you can say display the list of elements in forward direction. The method previous() of this class returns the previous element in the list or you can say in backward direction.