【发布时间】:2020-04-22 13:53:21
【问题描述】:
我正在开发一个危险的 GUI 游戏,我在其中对我的二维问题数组进行排序。我返回的 .compare 行出现错误。错误提示“方法 compare(String, 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有一个compare方法? 文档,即String的javadoc 没有显示这种方法。使用return a[0].compareTo(b[0]); -
arrays 和 user-interface 的相关性让我无法理解。
标签: java string sorting comparator