尝试在 Java 中使用自定义比较器时出错
Error when attempting to use custom comparator in Java
我正在尝试创建一个 class 来读取文本文件,其中每一行都具有相同的格式,“#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
而 class 是:
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)
.
BTW split
方法使用正则表达式 (regex) 作为参数,其中 )
是正则表达式 special characters. If you want to treat )
as literal you need to escape it (more info at Escape ( in regular expression)
之一
所以您可以使用 aScore.split("\) ");
而不是 aScore.split(") ");
我正在尝试创建一个 class 来读取文本文件,其中每一行都具有相同的格式,“#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
而 class 是:
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)
.
BTW split
方法使用正则表达式 (regex) 作为参数,其中 )
是正则表达式 special characters. If you want to treat )
as literal you need to escape it (more info at Escape ( in regular expression)
所以您可以使用 aScore.split("\) ");
aScore.split(") ");