Opening Hours :7AM to 9PM
In java language four mechanisims are available to retrieve the elements from any collection object.
1.Foreach Loop
2.Iterator Interface
3.Listiterator Interface
4.Enumeration Interface
This is similar to for loop .It is used to retrieve all elements of any collection object.
Syntax:
for(variable : collection_object) { ... ... }In the above syntax collection object always checks any elements are available in the given list or not.
import java.util.*; class ForEachLoop { void list() { ArrayList<Integer> al=new ArrayList<Integer>(); al.add(20); al.add(30); al.add(40); al.add(10); al.add(70); for(int i : al) { if(i>=30) { System.out.println("The List Items Are "+i); } } } } public class ArrayListDemo { public static void main(String aargs[]) { ForEachLoop d=new ForEachLoop(); d.list(); } }
Iterator objref=collectionobject.iterator()
import java.util.*; class IteratorDemo { void list() { ArrayList<Integer> al=new ArrayList<Integer>(); al.add(20); al.add(30); al.add(40); al.add(10); al.add(70); Iterator i=al.iterator(); while(i.hasNext()) { System.out.println("The List Items Are "+i.next()); } } } public class ArrayListDemo { public static void main(String aargs[]) { IteratorDemo d=new IteratorDemo(); d.list(); } }
ListIterator objref=collectionobject.listIterator()
import java.util.*; class IteratorDemo { void list() { ArrayList<Integer> al=new ArrayList<Integer>(); al.add(20); al.add(30); al.add(40); al.add(10); al.add(70); ListIterator i=al.listIterator(); while(i.hasNext()) { System.out.println("The List Items Are "+i.next()); } System.out.println("\n"); while(i.hasPrevious()) { System.out.println("The List Items Are "+i.previous()); } } } public class ArrayListDemo { public static void main(String aargs[]) { IteratorDemo d=new IteratorDemo(); d.list(); } }
StringTokenizer st=new StringTokenizer(String value,"any special symbol");
import java.util.*; public class TokenizerDemo { public static void main(String aargs[]) { String s="Hello students ,welcome to msk technologies"; StringTokenizer st=new StringTokenizer(s,","); while(st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }
import java.util.*; public class TokenizerDemo { public static void main(String aargs[]) { String s="Hello students ,welcome to msk technologies"; StringTokenizer st=new StringTokenizer(s," "); while(st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }
import java.util.*; public class TokenizerDemo { public static void main(String aargs[]) { String s="Hello students ,welcome to msk technologies"; StringTokenizer st=new StringTokenizer(s," "); int count=0; while(st.hasMoreTokens()) { count++; st.nextToken(); } System.out.pritln("Number of Words are"+ count); } }
import java.util.*; public class Main { public static void main(String[] args) { Enumeration<Integer> classNine; Vector<Integer> rollno = new Vector<Integer>(); rollno.add(1); rollno.add(2); rollno.add(3); rollno.add(4); rollno.add(5); rollno.add(6); rollno.add(7); rollno.add(8); classNine = rollno.elements(); while (classNine.hasMoreElements()) { System.out.println("Roll No = " + classNine.nextElement()); } } }