[英]Error when attempting to use custom comparator in Java
I am trying to create a class that reads a text file with each line having the same format, "#order) score - Name". 我正在尝试创建一个读取文本文件的类,每行具有相同的格式,“#order”得分 - 名称“。 I want to be able to read each line from a file and store it in an array, and then sort it by the highest score.
我希望能够从文件中读取每一行并将其存储在一个数组中,然后按最高分数对其进行排序。 I was able to accomplish this when it was just "#order) score", but adding the string to the mixture has complicated things.
当它只是“#order”得分时我能够完成这个,但是将混合物添加到混合物中会让事情变得复杂。 The error is
错误是
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>)
The text file is: 文本文件是:
1) 10000 - Michael
2) 10000 - Jake
3) 10000 - Alex
and the class is: 而且课程是:
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();
}
}
Any feedback is greatly appreciated! 非常感谢任何反馈!
Arrays.sort
is expecting T[]
but you are providing ArrayList
which is not array, it is a list which is using array internally to hold elements. Arrays.sort
期待T[]
但是你提供的ArrayList
不是数组,它是一个在内部使用数组来保存元素的列表。
Use Collections.sort
instead, or since Java 8 you can use yourList.sort(comparator)
. 请改用
Collections.sort
,或者从Java 8开始,您可以使用yourList.sort(comparator)
。
BTW split
method is using regular expression (regex) as argument where )
is one of regex special characters . BTW
split
方法使用正则表达式(regex)作为参数,其中)
是正则表达式特殊字符之一 。 If you want to treat )
as literal you need to escape it (more info at Escape ( in regular expression ) 如果你要正确对待
)
作为文字,你需要逃避它(更多信息逃亡(正则表达式 )
So instead of aScore.split(") ");
所以代替
aScore.split(") ");
you can use aScore.split("\\\\) ");
你可以使用
aScore.split("\\\\) ");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.