aspose 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

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterating over a (kind of) linked list" Watch "Iterating over a (kind of) linked list" New topic

Iterating over a (kind of) linked list

John Tupple

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

Joined: Oct 27, 2005
Posts: 19896

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

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40978
Welcome to the Ranch

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