1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
/* Iterate through elements Java ArrayList using ListIterator Example This Java Example shows how to iterate through the elements of java ArrayList object in forward and backward direction using ListIterator. */ import java.util.ArrayList; import java.util.ListIterator; public class IterateThroughArrayListUsingListIteratorExample { public static void main(String[] args) { //create an ArrayList object ArrayList arrayList = new ArrayList(); //Add elements to Arraylist arrayList.add("1"); arrayList.add("2"); arrayList.add("3"); arrayList.add("4"); arrayList.add("5"); /* Get a ListIterator object for ArrayList using listIterator() method. */ ListIterator itr = arrayList.listIterator(); /* Use hasNext() and next() methods of ListIterator to iterate through the elements in forward direction. */ System.out.println("Iterating through ArrayList elements in forward direction..."); while(itr.hasNext()) System.out.println(itr.next()); /* Use hasPrevious() and previous() methods of ListIterator to iterate through the elements in backward direction. */ System.out.println("Iterating through ArrayList elements in backward direction..."); while(itr.hasPrevious()) System.out.println(itr.previous()); } } /* Output would be Iterating through ArrayList elements... Iterating through ArrayList elements in forward direction... 1 2 3 4 5 Iterating through ArrayList elements in backward direction... 5 4 3 2 1 */ |
thx 4 helping me learning java.. anyway.. i don’t know how to start to learn java.. can you tell me the steps of learning.. coz 2 learn all about java almost impossible to me.. so please guid me.. the steps and the important part of java.. tq
first u install JDK and IDE for executing the java program .Then search some simple and sample java projects,and execute it…