Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Breadth first Search:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Breadth first Search:

    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?


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Breadth first Search:

    It means you cannot create an array of a generic type.

    You could try perhaps:

    ArrayList<ArrayList<int>>

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Breadth first Search:

    This is how you construct an ArrayList in java:
    List<Integer> myIntList = new ArrayList<>();
    you can not use brackets "[]" on anything but primitive arrays.

    If you want to iterate a list there are 2 possibilities, iterate with an index variable:
    for (int id = 0; id < myIntList.size(); id++) {
         Integer element = myIntList.get(id);
    }
    or a for-each loop over the elements:
    for (Integer element : myIntList) {...}

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Breadth first Search:

    Or, you can learn about the Java Collections Framework and the Iterator options it provides.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Breadth first Search:

    Thank you...

Similar Threads

  1. Depth-first search
    By tuts73 in forum Algorithms & Recursion
    Replies: 1
    Last Post: October 12th, 2012, 10:50 AM
  2. Sorting/Search
    By dx8292 in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 14th, 2012, 04:09 AM
  3. Graph Search Theory with extend of BFS, UFS and A* Search in grid
    By keat84 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 7th, 2012, 09:46 AM
  4. search file by id
    By d..:) in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 13th, 2011, 09:34 AM
  5. Breadth First Search Problem
    By Flash8 in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 14th, 2010, 07:27 PM