Is there any way to skip the first node while using an iterator?

425 views Asked by At

I have this code I'm using for implementing BFS as given below :

for(int i = 0; i < adjList.length; i++)
            {
                if(adjList[i].get(0).name.equals(u.name))
                {
                    for(Iterator <Vertex> it = adjList[i].iterator(); it.hasNext();)
                    {
                        Vertex node = it.next();
                        System.out.println("====="+node.name);
                        if(node.color.equals("WHITE"))
                        {
                            node.color = "GRAY";
                            node.distance = u.distance + 1;
                            node.pre = u;
                            Q.add(node);
                        }
                    }
                    u.color = "BLACK";
                    System.out.println();
                }
            }

I have implemented adjacency list using Lists of List using the following code :

ArrayList<Vertex> adjList[] = (ArrayList<Vertex>[])new ArrayList[size];

and the values stored inside the adjacency list are :

        adjList[0].add(new Vertex("r"));
        adjList[0].add(new Vertex("s"));
        adjList[0].add(new Vertex("v"));

        adjList[1].add(new Vertex("s"));
        adjList[1].add(new Vertex("r"));
        adjList[1].add(new Vertex("w"));

        adjList[2].add(new Vertex("t"));
        adjList[2].add(new Vertex("w"));
        adjList[2].add(new Vertex("x"));
        adjList[2].add(new Vertex("u"));

Inside the iterator loop, I need the object "node" to store every consecutive values except the first value, is it possible?

4

There are 4 answers

0
Eran On

There are several ways to skip the first element of the iterator.

For example :

boolean first = true;
for(Iterator <Vertex> it = adjList[i].iterator(); it.hasNext();) {
    if (first) { // skip first
        it.next();
        first = false;
    } else {
        // handle the rest
    }
}
0
wpodgorski On

Since you iterate over a List<Vertext> you can use listIterator(int index) method that is described here: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#listIterator(int). So your implementation would look like this:

for(int i = 0; i < adjList.length; i++) {
    if(adjList[i].get(0).name.equals(u.name)) {
        for(Iterator <Vertex> it = adjList[i].listIterator(1); it.hasNext();) {
            System.out.println("====="+node.name);
            if(node.color.equals("WHITE")) {
                node.color = "GRAY";
                node.distance = u.distance + 1;
                node.pre = u;
                Q.add(node);
            }
        }
    u.color = "BLACK";
    System.out.println();
    }
}
0
Marko Topolnik On

I would suggest a helper method:

<T> Iterator<T> skipFirstIterator(Iterable<T> iterable) {
   Iterator<T> it = iterable.iterator();
   it.next();
   return it;
}

And call that in your for-loop header.

The method impliciltly asserts the existence of the first element and throws an exception otherwise. I believe this is how it should be, given your program logic. If your list may legitimately have zero elements, then simply replace

it.next();

with

if (it.hasNext()) it.next();
0
Bohemian On

Rather than hack the iterator, why not just use subList():

for (Vertex node : adjList[i].subList(1, adjList[i].size())) {
    //
}

Note how your code doesn't need access to the iterator, so a foreach loop does nicely.