我正在研究危险的GUI游戏,在其中我对2D问题进行排序。我返回的.compare行出现错误。错误显示“类型为String的方法compare(String,String)未定义”
JButton btnSort = new JButton("Sort");
btnSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[][] questions = new String[][] { {"How many continents are there?"}, {"What is the capital of Canada?"}, {"What is the largest country in the world?"}, {"What is the largest ocean in the world?"}, {"How many oceans are there in the world?"}, {"How many countries make up Africa?"}, {"How many countries in the world begin with the word United?"}, {"Where is Milan?"}, {"What is the least populated US state?"}, {"What is the capital of Australia?"}, {"How many countries begin with the letter J?"}, {"Which country has the most lakes in the world?"}};
java.util.Arrays.sort(questions, new java.util.Comparator<String[]>() {
public int compare(String[] a, String[] b) {
return String.compare(a[0], b[0]);
}
});
}
});
因为String.compareTo(String)
是不是一种采用两个参数(和未命名为比较)的String.compareTo(String)
方法。这是一个实例方法,可以将一个static
实例与另一个实例进行比较;喜欢,
String
这是因为您将compare方法用作静态方法。像其他任何实例方法一样使用它。您甚至可以使用compareTo()。