[英]How do I compare two lists in Java and print the result based on each combination?
我想根据价格矩阵计算每个车站之间的票价:
a b c
a 0 2 3
b 4 0 1
c 7 2 0
示例:根据上述价格矩阵中的值, from a to b
打印2
或from c to a
打印7
。
就是这样,我想根据两个站点列表打印火车票票价:“from:”列表和“to:”列表。 我想在比较后打印票价。 每种组合都有固定票价。 例如a站到b站,票价是10。对于任何一个站到另一个站,都有固定的票价。
我将创建一个 class,它负责存储票价。
public class FareStorage {
Map<TownCombination, Double> fares;
//...
public double getFare(String townA, String townB) {
return fares.get(new TownCombination(townA, townB));
}
public void addFare(String townA, String townB, double fare) {
fares.put(new TownCombination(townA, townB));
}
class TownCombination {
String town1;
String town2;
//If a fare from A to B is equals the fare from B to A,
//then the A-B and the B-A combinations should be equal.
//Override hashCode and equals the way you want.
}
}
它不完整,但我希望你能明白。 这是您可以使用它的方式:
FareStorage storage = new FareStorage();
storage.addFare("A", "B", 10.2);
//....
double fare = storage.get("A", "B");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.