This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
So here's a snippet of something I'm working on, trying to learn more about ArrayLists. It's a simple breadth-first search algorithm. Things are going well, however for some reason, when I try to initialize the "adjacent" ArrayList to the product of running adjacentVertices, it always initializes to 0. (line 23) Adjacent vertices works great by itself, it produces an ArrayList of all nodes adjacent to the node I give it, however it doesn't pass that result to my variable.
It will initialise to 0 as in the first iteration of your for loops, n = 0. It might be worth running through the pen and paper version without thinking in java to work out what should be occuring before coding further.
Joined: Oct 05, 2012
I guess I should have posted my adjacentVertices code, I apologize, it sends 0 to adjacent vertices which gives me all the vertices adjacent to node "0". So without all the previous stuff, if I just run adjacentVertices(0), it gives me my arraylist [1 2 3]. So when I was setting my arraylist=adjacentVertices(n) (which, you're correct, n would be zero), I'm wanting arraylist to be initialized to [1 2 3], but for some reason when I debug, it's still at 0.
Have a look at the javadoc for ArrayList, in particular how to add items to an ArrayList. If I'm reading it right, your adjacentVertices method is going to return an ArrayList of integers, which you then want to add them all to the ArrayList adjacent. Also, if you debug what does the adjacentVertices return variable contain? Ensure that this variable contains the correct values you want returned.
Stuie Clarky wrote:It will initialise to 0 as in the first iteration of your for loops, n = 0. It might be worth running through the pen and paper version without thinking in java to work out what should be occuring before coding further.
It is always worth running through the pen and paper version without thinking in Java. ;)