我有一个DurationBetween类,它表示原始位置和目的地之间的旅行持续时间。该类具有三个属性,分别为int dur
,String orig
和String dest
。
我正在尝试获取一组位置之间的持续时间。我已经为此做了计算。
我有3个看起来像这样的数组列表:
listOrig: current, dest1, dest2, dest3
listDest: dest1, dest2, dest3
listDur: 2, 3, 5, 0, 1, 3, 9, 0, 2, 3, 2, 0
通过将持续时间从
listDur
到current
,dest1
,dest2
,然后从dest3
到dest1
,dest1
,dest2
,然后从dest3
到,dest2
,dest1
等。总共12个。请注意,当原始位置与目的地相同时,duration = 0。
现在,我需要启动一堆
dest2
类来存储dest3
和DurationBetween
对。所以像DurationBetween durationBetween = new DurationBetween(2, current, dest1);
DurationBetween durationBetween = new DurationBetween(3, current, dest2);
DurationBetween durationBetween = new DurationBetween(5, current, dest3);
DurationBetween durationBetween = new DurationBetween(1, dest1, dest2);
...
我在初始化类时遇到问题,因为列表大小不同。我现在拥有的代码为我提供了orig和dest对,但是我无法让dur相应地更新。
这就是我所拥有的:
// create classes of dur, orig, dest
// one orig will go to multiple dest and have multiple dur
for (int i = 0; i < listOrig.size(); i++) {
for (int j = 0; j < listDest.size(); j++) {
String orig = listOrig.get(i);
String dest = listDest.get(j);
// DEFINITELY NOT RIGHT
for (int k = 0; k < listDur.size(); k++) {
int dur = Integer.parseInt(listDur.get(k));
if (dur != 0) { // skip instances where orig and dest are the same
DurationBetween durationBetween = new DurationBetween(dur, orig, dest);
listDurBtwn.add(durationBetween);
}
}
}
}
public class DurationBetween {
private int dur;
private String orig;
private String dest;
public DurationBetween(int dur, String orig, String dest) {
this.dur = dur;
this.orig = orig;
this.dest = dest;
}
}
最佳答案
我认为您需要用以下内容替换“绝对不正确”的部分:
int k = (i * listDest.size()) + j;
int dur = Integer.parseInt(listDur.get(k));
if (dur != 0) { // skip instances where orig and dest are the same
DurationBetween durationBetween = new DurationBetween(dur, orig, dest);
listDurBtwn.add(durationBetween);
}