private class AdjacentVerticesIterator implements VertexIterator { int nextPos = -1; int vertex; AdjacentVerticesIterator(int v) { vertex = v; findNext(); } private void findNext() { nextPos++; while (nextPos < numVertices && !edgeCosts.containsKey(new Edge(vertex, nextPos))) nextPos++; } public boolean hasNext() { return nextPos < numVertices; } public int next() { int pos = nextPos; if (pos < numVertices) { findNext(); return pos; } throw new NoSuchElementException( "This iterator has no more elements."); } }