【发布时间】:2019-07-18 07:40:45
【问题描述】:
我从一个 double 类型的文本文件中打印了数据,并将其转换为一个看起来像这样的 double 数组
[-2.0, -2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 1.39E-04
-2.0, 0.0, -2.0, -2.0, 0.0, -2.0, 2.0, 0.020446
0.0, -2.0, 2.0, 2.0, -2.0, -2.0, -2.0, 0.032339
2.0, -2.0, -2.0, 2.0, 2.0, -2.0, 2.0, 0.026673
0.0, -2.0, -2.0, 0.0, 2.0, 0.0, 2.0, 0.094135
0.0, 0.0, -2.0, 0.0, 2.0, 2.0, 0.0, 0.045922
-2.0, 0.0, -2.0, 0.0, 2.0, 0.0, -2.0, 0.117043
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, -2.0, 0.425709
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.156286]
最后一列是根据特定公式计算的每一行的分数。我的问题是如何根据最后一列的最大值对这个数组列表进行排序? 排序后的数组的输出应该是这样的
-2.0, -2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 1.39E-04
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, -2.0, 0.425709
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.156286
-2.0, 0.0, -2.0, 0.0, 2.0, 0.0, -2.0, 0.117043
0.0, -2.0, -2.0, 0.0, 2.0, 0.0, 2.0, 0.094135
0.0, 0.0, -2.0, 0.0, 2.0, 2.0, 0.0, 0.045922
0.0, -2.0, 2.0, 2.0, -2.0, -2.0, -2.0, 0.032339
2.0, -2.0, -2.0, 2.0, 2.0, -2.0, 2.0, 0.026673
-2.0, 0.0, -2.0, -2.0, 0.0, -2.0, 2.0, 0.020446
到目前为止我尝试过的代码
ArrayList<String> a2 = new ArrayList<String>();
File file7 = new File("kk.txt");
BufferedWriter output7 = new BufferedWriter(new FileWriter(file7));
output7.write(array+"");
output7.close();
Scanner s = new Scanner(new FileReader("kk.txt"));
while (s.hasNextLine()) {
String line1 = s.nextLine();
//store this line to string [] here
line1 = line1.replaceAll("\\[", "");
line1 = line1.replaceAll("\\]", "");
line1= line1.replaceAll("\\;,","\r\n"+"");
line1= line1.replaceAll("\\;","\r\n"+"");
a2.add(line1);
}
Collections.sort(a2,Collections.reverseOrder());
System.out.println("Sorted List : " + a2);
但代码返回数组而不进行排序。任何帮助表示赞赏
【问题讨论】:
-
您应该首先将每一行拆分为单独的
double值,然后使用自定义Comparator进行排序。 -
您可以使用标准文件格式吗? (例如 csv rfc4180)
-
谢谢@PavelSmirnov,你能用代码告诉我吗,因为我还是初学者