This question already has answers here:
What is a debugger and how can it help me diagnose problems?

(2个答案)


4年前关闭。





我有一组以下类型的输入字符串,

String[] arr = {"pear", "amleth", "dormitory", "tinsel", "dirty room", "hamlet", "listen", "silent"};


我需要编写一个程序来检查这些字符串中的哪些是字谜,然后将它们打印在逗号分隔的列表中,按字典顺序排序。因此,预期输出为

amleth, hamlet
dirty room, dormitory


.......

这是我的代码

public class Main {

    static void checkPrintAnagrams(String[] str){

        List<List<String>> out = new ArrayList<>();

        int[] check = new int[str.length];
        for(int i = 0; i < str.length; i++){
            List<String> list = new ArrayList<>();
            for(int j= 1; j < str.length; j++){
                if(check[j] != 1 && check[i] != 1){
                   if(isAnagram(str[i], str[j])){
                       list.add(str[i]);
                       list.add(str[j]);
                       check[j] = 1;
                       check[i] = 1;
                   }
                }
            }
            out.add(list);
        }

        Collections.sort(out, new Comparator<List<String>> () {
            @Override
            public int compare(List<String> a, List<String> b) {
                return a.get(1).compareTo(b.get(1));
            }
        });

        for(Iterator itr = out.iterator(); itr.hasNext();){
            List<String> l = (List<String>) itr.next();
            for(Iterator it = l.iterator(); it.hasNext();){
                System.out.print(it.next() + ",");
            }
            System.out.println();
        }
    }

    static boolean isAnagram(String firstWord, String secondWord) {
        char[] word1 = firstWord.replaceAll("[\\s]", "").toCharArray();
        char[] word2 = secondWord.replaceAll("[\\s]", "").toCharArray();
        Arrays.sort(word1);
        Arrays.sort(word2);
        return Arrays.equals(word1, word2);
    }


    public static void main(String[] args) {
    // write your code here
        String[] arr = {"pear", "amleth", "dormitory", "tinsel", "dirty room", "hamlet", "listen", "silent"};
        checkPrintAnagrams(arr);
    }
}


这段代码中的列表列表部分的排序是我在网络上没有完全理解的东西,就像没有完全理解的东西都以out of bound exception结尾。

这是我的错误信息。

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at io.soumasish.Main$1.compare(Main.java:31)
    at io.soumasish.Main$1.compare(Main.java:28)
    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    at java.util.TimSort.sort(TimSort.java:220)
    at java.util.Arrays.sort(Arrays.java:1512)
    at java.util.ArrayList.sort(ArrayList.java:1454)
    at java.util.Collections.sort(Collections.java:175)
    at io.soumasish.Main.checkPrintAnagrams(Main.java:28)
    at io.soumasish.Main.main(Main.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


希望能帮助您理解Collections sort部分以及如何在这种情况下正确实现它。

最佳答案

您可以使用StreamgroupingBy创建Map<String, List<String>>

您需要做的只是按以下几种归一化数据对数据进行分组:


删除所有空格
按字母顺序对字符进行排序
创建一个String


简单:

Map<String, List<String>> anagrams = Stream.of(arr).collect(groupingBy(s -> {
    char[] chars = s.replaceAll("\\s", "").toCharArray();
    Arrays.sort(chars);
    return new String(chars);
}));


例如:

anagrams.forEach((k, v) -> {
    System.out.printf("Anagrams of %s - %s%n", k, v);
});


输出:


eilnst Anagrams-[金属丝,听,无声]
二元字谜-[宿舍,肮脏的房间]
aehlmt字谜-[amleth,hamlet]
aepr字谜-[梨]

关于java - 检查和打印字谜时出现IndexOutOfBoundsException ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35538395/

10-10 14:29