Implementation of iterator pattern for any array of elements I have implemented Iterator pattern:

The interface Container.java:

public interface Container&lt;T&gt; {

    Iterator&lt;T&gt; getIterator();
}


Interface Iterator.java:

public interface Iterator&lt;T&gt;{

    boolean hasNext();

    T next();
}


Class ResourceContainer.java:

public class ResourceContainer&lt;T extends Serializable&gt; implements Container&lt;T&gt;{

    private T names [];

    public ResourceContainer(T [] names){
        this.names = names;
    }

    @Override
    public Iterator&lt;T&gt; getIterator() {      
        return new ResourceIterator&lt;T&gt;(this.names);
    }

}


Class ResourceIterator.java:

public class ResourceIterator&lt;T extends Serializable&gt; implements Iterator&lt;T&gt;{

    private T [] names;
    private int index;

    public ResourceIterator(T [] names){
        this.names = names;
    }

    @Override
    public boolean hasNext() {
        boolean flag = false;

        if(this.index &lt; this.names.length){
            flag = true;
        }

        return flag;
    }

    @Override
    public T next() {               
        return this.names[index++];
    }

}


An EmployeeVO class:

import java.io.Serializable;

public class EmployeeVO implements Serializable{

    private String name;
    private Integer id;
    private String departmentName;

    public EmployeeVO(Integer id, String name, String departmentName){
        this.name = name;
        this.id = id;
        this.departmentName = departmentName;
    }

    @Override
    public String toString(){
        StringBuffer strb = new StringBuffer();

        strb.append(" Id \"").append(this.id).append("\"")
            .append(" Name \"").append(this.name).append("\"")
            .append(" Department \"").append(this.departmentName).append("\"");

        return strb.toString();     
    }
}


Main class:

public class MainClass {    

    public static void main(String [] args){        

        Integer [] nums = new Integer[]{12,23,100,1};

        Container&lt;Integer&gt; container01 = new ResourceContainer&lt;Integer&gt;(nums);
        Iterator&lt;Integer&gt; itr01 = container01.getIterator();

        while(itr01.hasNext()){
            System.out.println(itr01.next());
        }

        String [] names = new String[]{ "Alan", "Jim", "Nicholas" };

        Container&lt;String&gt; container02 = new ResourceContainer&lt;String&gt;(names);
        Iterator&lt;String&gt; itr02 = container02.getIterator();

        while(itr02.hasNext()){
            System.out.println(itr02.next());
        }

        EmployeeVO [] employees = new EmployeeVO[] {
        new EmployeeVO(23, "Alan Turing", "Computer Science"),
        new EmployeeVO(45, "Jim Klaus", "Mechanical Engineering"),
        new EmployeeVO(76, "Nicholas Johanson", "Electrical Engineering")
        };

        Container&lt;EmployeeVO&gt; container03 = new ResourceContainer&lt;EmployeeVO&gt;(employees);
        Iterator&lt;EmployeeVO&gt; itr03 = container03.getIterator();

        while(itr03.hasNext()){
            System.out.println(itr03.next());
        }
    }
}


The output of when I run MainClass from the command line:



12
23
100
1
Alan
Jim
Nicholas
Id "23" Name "Alan Turing" Department "Computer Science"
Id "45" Name "Jim Klaus" Department "Mechanical Engineering"
Id "76" Name "Nicholas Johanson" Department "Electrical Engineering"


By following the code, please review and let me know whether I implemented the iterator pattern properly. Is the usage of generics proper? Can the implementation be further improved?
I have two points for improvement:


Right now next() method increments the index. What happens if someone calls next() without calling hasNext() method?


Take care of ArrayIndexOutOfBoundsException or increment the index carefully.

You could implement remove() method (Optional)