-1

Hi I am new to Java so please using basic and simple Java methods that will help me quickly understand your idea.

Problem: I have n cities (each city has a unique names) and they are all connected to each other so that there is a distance between any 2 cities.

What is the best way to store those distances so later if I use name of 2 cities (since name is unique) I can retrieve distance between them?

I was thinking about using 2-D array but it doesn't seem like a good idea (possible duplication distance between A - B and B - A, also can't using city names) does it?

Why did somebody give thumb-downs to this question?

4

3 に答える 3

2

あなた自身のアイデアに追加する2つの可能性

HashMapのHashMap -2D配列よりも重いですが、都市名から直接、使いやすさを提供します。

または、enum都市の名前でを作成し、を使用enumして2D配列にインデックスを付けます。

可変次元の2DArray(非長方形) -各行は異なるサイズを持つことができ、完全な行列の半分のみを格納し、必要に応じて残りの半分を導出します。例:非長方形配列の作成

于 2013-02-25T06:27:08.893 に答える
0

If the distance between two cities is just the geometric distance, you only need to store the coordinates of each city.

Otherwise, store the distances in a N*N matrix, and keep an String[N] with the names.

于 2013-02-25T06:26:09.470 に答える
0

Javaのコレクションの概念を調べてください。これは、探しているものを実装するのに役立つはずです。

http://docs.oracle.com/javase/tutorial/collections/index.html

于 2013-02-25T06:27:22.963 に答える