gpt4 book ai didi

java - 尝试在 Java 中使用自定义比较器时出错

转载 作者:行者123 更新时间:2023-11-29 04:32:30 25 4
gpt4 key购买 nike

我正在尝试创建一个读取文本文件的类,该文件的每一行都具有相同的格式,“#order) score - Name”。我希望能够从文件中读取每一行并将其存储在一个数组中,然后按最高分排序。当它只是“#order) score”时,我能够做到这一点,但是将字符串添加到混合物中会使事情变得复杂。错误是

Scoring.java:46: error: no suitable method found for sort(ArrayList<String>,<anonymous Comparator<String>>)
Arrays.sort(scores, new Comparator<String>(){
^
method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable
(cannot infer type-variable(s) T#1
(argument mismatch; ArrayList<String> cannot be converted to T#1[]))
method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>sort(T#1[],Comparator<? super T#1>)
T#2 extends Object declared in method <T#2>sort(T#2[],int,int,Comparator<? super T#2>)

文本文件是:

1) 10000 - Michael
2) 10000 - Jake
3) 10000 - Alex

类是:

import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
public class Scoring
{
private ArrayList<String> scores = new ArrayList<String>();
public Scoring()
{
this(-1, "-1");
}
public Scoring(int newScore, String newName)
{
String highScoreFile = "scores.txt";
String line;

BufferedReader reader = null;
try
{
File file = new File(highScoreFile);
reader = new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}

finally
{
try
{

while ((line = reader.readLine()) != null)
{
String aScore = line;
String segments[] = aScore.split(") ");
aScore = segments[segments.length - 1];
scores.add(aScore);
}
if (newScore != -1 && !newName.equals("-1"));
{
scores.add(newScore + " - " + newName);
}
System.out.println(Arrays.toString(scores.toArray()));
Arrays.sort(scores, new Comparator<String>(){
@Override
public int compare(String o1, String o2)
{
return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
}
});

BufferedWriter writer = null;
try
{
File file = new File(highScoreFile);
FileWriter fw = new FileWriter(file);
writer = new BufferedWriter(fw);

}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
for (int i = 0; i < scores.size(); i++)
{
writer.write((i + 1) + ") ");
writer.write(scores.get(i) + "\n");
}
System.out.println(Arrays.toString(scores.toArray()));
reader.close();
writer.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
public static void main(String[] args) {
Scoring score = new Scoring();


}
}

非常感谢任何反馈!

最佳答案

Arrays.sort期待T[]但是你提供了ArrayList这不是数组,它是一个在内部使用数组来保存元素的列表。

使用 Collections.sort相反,或者从 Java 8 开始,您可以使用 yourList.sort(comparator) .


顺便说一句 split方法使用正则表达式 (regex) 作为参数,其中 )是正则表达式之一 special characters .如果你想治疗)作为文字你需要转义它(更多信息在 Escape ( in regular expression )

所以不是 aScore.split(") ");你可以使用 aScore.split("\\) ");

关于java - 尝试在 Java 中使用自定义比较器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com