-2

好的,所以基本上我的程序中有 2 个多维数组:

生成的距离:空

999.0   22.0    38.0    9.0     12.0

10.0    999.0   9.0     22.0    24.0

10.0    41.0    999.0   25.0    49.0

4.0     2.0     18.0    999.0   1.0

4.0     38.0    8.0     20.0    999.0

道路矩阵:

null     town     town       national   regional
town     null     national   motorway   motorway
town     motorway null       regional   town
motorway national national   null       town
town     town     national   regional   null

第一个数组生成距离,而第二个数组生成这些距离的道路类型。

我正在使用一种算法来计算最佳路线,我正在处理第一个矩阵。我使用了一个数组列表。

[0, 4, 3, 1, 2, 0]

基本上我想在我的第二个数组中获取道路索引。

结果应如下所示[区域,区域,国家,国家,城镇]

我只是想知道使用第一个矩阵生成的结果最有效的方法是什么?

谢谢

4

1 回答 1

0

由于您使用第一个数组中的数据来填充第二个数组,因此最好将它们保持在彼此附近,以防止尽可能多的缓存未命中。

例如:

public class Road {
  public float distance;
  public RoadType roadType;
};

// if you know the size, try using an actual array of Road
List<Road> roads;

除非您想提高访问该数据的效率——否则您可能需要更改数据的内存布局。(在上述情况下,它应该在创建时高效)

于 2013-08-12T16:00:32.310 回答