gpt4 book ai didi

java - 检查和打印字谜时出现 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 08:55:48 28 4
gpt4 key购买 nike

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

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部分以及如何在这种情况下正确实现它。

最佳答案

您可以使用 Stream groupingBy 创建 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);
});

输出:

Anagrams of eilnst - [tinsel, listen, silent]
Anagrams of dimoorrty - [dormitory, dirty room]
Anagrams of aehlmt - [amleth, hamlet]
Anagrams of aepr - [pear]

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com