File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Iterating over a (kind of) linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterating over a (kind of) linked list" Watch "Iterating over a (kind of) linked list" New topic
Author

Iterating over a (kind of) linked list

John Tupple
Greenhorn

Joined: Jul 28, 2010
Posts: 1
Hi Everyone,

I am facing a small challenge that I'm hoping someone can help with. I have built a model of a city map that contains cities that each contain a list of other cities they are connected to, like this:



I am using the map to calculate routes that contain four, five or more cities. I am concerned that my method is not very efficient, it involves iterating over multiple arrays and 'moving' backwards and forwards over the map. My questions is whether there is an efficient way to calculate the route through 'n' cities, given 'n' can change.

Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Look up the traveling salesman problem. And no, it's not going to be efficient.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Welcome to the Ranch

That isn't called a linked list, more likely a "graph".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating over a (kind of) linked list