Java Examples

Description in welcome.jsp

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

Program

import java.util.ArrayList;
import java.util.Iterator;
 
public class Iterater {
    public static void main(String[] args) {
        ArrayList al = new ArrayList();
        // add elements to the array list
        al.add("C");
        al.add("A");
        al.add("E");
        al.add("B");
        al.add("D");
        al.add("F");
 
        // Use iterator to display contents of al
        Iterator itr = al.iterator();
        while (itr.hasNext()) {
            Object element = itr.next();
        }
 
    }
 
}

Output
Original contents of al: C A E B D F

Explanation
An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: § Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. § Method names have been improved. Declaration Iterator iterator() Parameters E – The type of elements returned by this iterator Return Value String Exception NoSuchElementException – if the iteration has no more elements UnSupportedOperationException – if the remove operation is not supported by this iterator IllegalStateException- if the next method has not yet been called, or the remove method has already been called after the last call to the next method