【发布时间】:2014-10-15 14:01:04
【问题描述】:
我在大学课程方面遇到了一些问题。我也完成了作业的最后一部分,我认为容易的部分很快就变成了最难的部分。我被要求按距离对 .cvs 文件中的值进行排序(17.0123874627346,0.123761947 等值)
这是我的代码:
public void sortDistance() {
System.out.println("before distance sort");
for (int k = 0; k < ntracks; k++) {
System.out.println(tracks[k].getDistance());
}
for (int i = 0; i < ntracks; i++) {
//find the index of the minimum position
int index = i;
//looping through array of distances
for (int j = i + 1; j < ntracks; j++) {
if (tracks[index].getDistance() < tracks[j].getDistance())
{
//position j is new mimum index
index = j;
}
}
//swap the two values around so smallest becomes track index
Track smallerTrack = tracks[index];
tracks[index] = tracks[i];
tracks[i] = smallerTrack;
}
System.out.println(" after distnace sort");
for (int k = 0; k < ntracks; k++) {
System.out.println(tracks[k].getDistance());
}
}
【问题讨论】: