Java Iterator getObjectAtIteratorPos(Iterator i, int pos)

Here you can find the source of getObjectAtIteratorPos(Iterator i, int pos)

Description

Given a numeric position, cycle through the given Iterator until the position is reached, and return the Object found there.

License

Open Source License

Parameter

Parameter Description
i The Iterator to be cycled through.
pos The numeric position of the Object to be returned

Return

The Object at the indicated position

Declaration

public static Object getObjectAtIteratorPos(Iterator<?> i, int pos) throws ArrayIndexOutOfBoundsException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**/*from w  w  w . j  a  v a2 s .  com*/
     * Given a numeric position, cycle through the given Iterator until the position
     * is reached, and return the Object found there.
     * 
     * @param i The Iterator to be cycled through.
     * @param pos The numeric position of the Object to be returned
     * @return The Object at the indicated position
     */
    public static Object getObjectAtIteratorPos(Iterator<?> i, int pos) throws ArrayIndexOutOfBoundsException {
        int currPos = 0;
        Object oc;
        while (i.hasNext()) {
            oc = i.next();
            if (currPos == pos)
                return oc;
            else
                currPos++;
        }
        throw new ArrayIndexOutOfBoundsException();
    }
}

Related

  1. fromIterator(Iterator itr)
  2. get(Iterator iterator, int position)
  3. getEnumeratedObjectCount(Iterator objects)
  4. getEnumeration(Iterator i)
  5. getIteratorEnumeration(final Iterator i)
  6. hasNext(final Iterator i)
  7. hasNext(final Iterator it)
  8. hasNextIteration(final Iterator iterator)
  9. hasNexts(List> heads)