I have done this before in C++ but now I want to do it in Java. I am not sure how to iterate through a ArrayList, I have look several places because lets be honest the first stop is normally google. I tried the examples but with no luck. I was hoping to find a fixed work like
for(auto it= foo.begin(); it!= foo.end(); foo++) { *it //do something. }
Here is the program:
package org.search.BFS; import java.util.Queue; public class BFS extends Graph { private Queue<Integer> q; BFS(int s) { //mark all the vertices as not visited boolean[] visited = new boolean[V]; for(int i=0; i <V; i++) { visited[i]=false; } //mark the current node as visited and enqueue it visited[s]=true; q.add(s); while(!q.isEmpty()) { //Dequeue a vertex from queue and print it s=q.poll();// gets front of queue System.out.println(s); q.remove();//popping front off queue //************************************************ //I want to iterate doing something like for(auto = adj[s].begin(); i != adj[s].end(); ++i) //************************************************* } } }
package org.search.BFS; import java.util.ArrayList; public class Graph { protected int V; ArrayList<Integer>[] adj; //array containing adjacency lists Graph() { } Graph(int V) //constructor { this.V = V; adj = new ArrayList[V]; } void addEdge(int v, int w) { adj[v].add(w); } }
Also the below is giving me a Type safety: The expression of type ArrayList[] needs unchecked conversation to conform to ArrayList<Integer>[]
adj = new ArrayList[V];
What does this mean?