想象一下列表,其中包含距离和属于它的时间(以秒为单位)。
meterTempos
{
distance: 500,
seconds: 50
},
{
distance: 600
seconds: 60
}
以上列表可以是任意数量的“距离 - 秒”项目。这只是一个非硬编码数据的例子。
我的问题是,如何将其转换为以下格式?
kmTempos
{
km: 1,
distance: 1000,
seconds: 100
}, {
km: 2,
distance: 100,
seconds: 10
}
这是我尝试的内容:
double totaldistance = 1100; //Total distance is variable, but for the sake of simplicity, I set it to 1.1
int loops = (int)(Math.Ceiling(totaldistance / 1000));
List<KilometerTempo> kmTempos = new List<KilometerTempo>();
for (int j = 0; j < loops; j++)
{
double seconds = meterTempos[j].seconds;
double distance = meterTempos[j].meters;
//this is definitely wrong, since it will add new KilometerTempos for every item in meterTempos
kmTempos.Add(new KilometerTempo(j + 1, distance, seconds));
}
正如您所看到的,我不明白如何迭代列表并使用每个新公里应考虑的子结果。
答案 0 :(得分:2)
const Subject&