0 likes | 33 Views
In Java mainly two types of cursors: Iterator and List Iterator.<br>Both are pre-defined Interfaces present in java.util package.<br>Iterator and List Iterator are used to traverse a group of objects.<br>List Iterator is used to traverse objects either in a forward direction or in a backward direction. i.e. bi-directional cursor.
E N D
Cursors in Java collection cursors in Java
Overview • In Java mainly two types of cursors: Iterator and List Iterator. • Both are pre-defined Interfaces present in java.util package. • Iterator and List Iterator are used to traverse a group of objects. • List Iterator is used to traverse objects either in a forward direction or in a backward direction. i.e. bi-directional cursor.
Program import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String[] args) { ArrayListaList=new ArrayList<>(); aList.add(10); aList.add("Praveen"); aList.add(15.50); aList.add(21); Iterator i=aList.iterator(); while(i.hasNext()) { System.out.println(i.next()); } System.out.println("-------------------------------"); System.out.println("List before remove:\n"+aList); System.out.println("-------------------------------"); aList.remove(2);//Index 2 value is 12.20 System.out.println("List after remove:\n"+aList); } }
Output: 10 Praveen 15.5 21 ------------------------------- List before remove: [10, Praveen, 15.5, 21] ------------------------------- List after remove: [10, Praveen, 21]