Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraylist nuances

 
Jon Richards
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jon Richards
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic