我收到一个错误消息:“类型String的方法compare(String,String)未定义”。为什么我不能对2D数组排序? [关闭]

问题描述 投票:-2回答:2

我正在研究危险的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]);
                                }
                            });

                }
            });
java string sorting comparator
2个回答
2
投票

因为String.compareTo(String)不是一种采用两个参数(未命名为比较)的String.compareTo(String)方法。这是一个实例方法,可以将一个static实例与另一个实例进行比较;喜欢,

String

0
投票

这是因为您将compare方法用作静态方法。像其他任何实例方法一样使用它。您甚至可以使用compareTo()。

© www.soinside.com 2019 - 2024. All rights reserved.