我有两个ArrayList<String>
s。一个包含带有标点符号和大写的单词,另一个则删除了这些。
清单 1:
File word: it's
File word: Sit
File word: yes-man
File word: murdered
File word: ok
File word: DereDrum
File word: Hello
File word: Friend
清单 2:
Edited word: its
Edited word: sit
Edited word: yesman
Edited word: murdered
Edited word: ok
Edited word: deredrum
Edited word: hello
Edited word: friend
这个想法是检查并查看哪些单词是彼此的字谜。如果是的话,我想做的就是重新打印它们,但在同一行上的字谜词:
it's Sit
yes-man
murdered DereDrum
ok
hello
friend
我试着这样做:
for (int i = 0; i < words.size(); i++)
{
System.out.print(words.get(i));
for (int j = i+1; j < grams.size(); j++)
{
anagramChecker(grams.get(i),"",grams.get(j),words.get(j));
}
System.out.println();
}
我想到的方式是这个 for 循环持续 List1(words) 的大小并打印它正在查看的单词。然后我使用我的 anagramChecker:
public static void anagramChecker(String s1,String s2,String checked,String word)
{
if(s1.length() == 0)
{
if(s2.equals(checked))
{
System.out.println(" " + word);
return;
}
}
for(int i = 0 ; i < s1.length() ; i++)
{
anagramChecker(s1.substring(0, i) + s1.substring(i+1, s1.length()),s2 + s1.charAt(i),checked,word);
}
}
这种方法不起作用,它的输出是这样的:
it's
Sit yes-man
yes-man
yes-man
yes-man
yes-man
yes-man
yes-man
yes-man
yes-man
murdered
ok
DereDrum Hello
Hello
Friend
我尝试使用地图,以便在单词本身在第二个列表中按字母顺序排序后,我可以按字母顺序对单词进行排序。这具有大致相同的外观输出。