0
我试图理清一个ArrayList,但我不能换我的头周围比较。我不明白如何定义从文本文件创建的我的数组列表中的可排序字段。此外,我不确定比较器逻辑。在我看来,像创建一组比较函数,然后调用它们。这是真的?我想不通比较
到目前为止,我的代码如下所示:
public class coord implements Comparator<Sort> {
private int index;
private int index2;
private double dista;
}
public class Sort {
List<Sort> coords = new ArrayList<Sort>();
public static void main(String[] args) throws Exception {
ArrayList dist = new ArrayList();
File file = new File("2.txt");
FileWriter writer = new FileWriter("2c.txt");
try {
Scanner scanner = new Scanner(file).useDelimiter("\\s+");
while (scanner.hasNextLine())
{
int index = scanner.nextInt();
int index2 = scanner.nextInt();
double dista = scanner.nextDouble();
System.out.println(index + " " + index2 + " " + dista);
}
}
}
public class EmpSort {
static final Comparator<coord> SENIORITY_ORDER =
new Comparator<coord>() {
public int compare(coord e1, coord e2) {
return e2.index().compareTo(e1.index());
}
};
static final Collection<coord> coords = ;
public static void main(String[] args) {
List<Sorted>e = new ArrayList<Sorted>(coords);
Collections.sort(e, SENIORITY_ORDER);
System.out.println(e);
我感谢所有帮助任何人都可以给。
此代码是不完整的。尝试没有catch或finally子句。 – Srikanth 2011-04-21 20:27:48
我认为你有一堆模板。使用铅笔和纸张绘制您的对象系统,显示层次结构和关系。这将需要10分钟的时间,并清除你的想法。 – slezica 2011-04-21 20:29:05