This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arraylist nuances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arraylist nuances" Watch "Arraylist nuances" New topic
Author

Arraylist nuances

Jon Richards
Greenhorn

Joined: Oct 05, 2012
Posts: 10
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.

Any suggestions would be appreciated.

Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 76

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.


OCAJP 7
Jon Richards
Greenhorn

Joined: Oct 05, 2012
Posts: 10
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.
Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 76

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.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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. ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arraylist nuances