我正在查找如何执行字符串排列并找到下面的解决方案。但是我很难理解所使用的逻辑。尤其是这一行。for(final String permutation : Permuatations(subList(head,words)))据我所知,作者是在其自身“Permutations”上调用一个函数来执行其中的 subList 函数,这很难让我理解。谁能让我更清楚一点?任何指导将不胜感激。
public static void main (String [] args)
{
for(final String s: Permuatations(Arrays.asList("This ","is ","String ")))
{
System.out.println("6. THE FINAL OUTPUT " +s);
}
}
public static List<String> Permuatations(final List<String> words)
{
final List<String> perms = new ArrayList<String>();
if (words.size() == 1)
{
perms.add(words.get(0));
System.out.println("3. permuatations if words " + words);
System.out.println("4. PERMS LIST " + perms);
}
else
{
for(final String head : words)
{
for(final String permutation : Permuatations(subList(head,words)))
{
perms.add(head + permutation);
System.out.println("5 .SubList HEAD " + head + " PERMUATATION " + permutation + " Word Size " + words.size() );
}
}
}
return perms;
}
public static List<String> subList(final String elementToRemove, final List<String> elements)
{
final List<String> subList = new ArrayList<String>();
for(final String s : elements)
{
//System.out.println(" 1. STRING s " + s + " ELEMENTS " + elements);
if(!s.equals(elementToRemove))
{
System.out.println(" 1. STRING S " + s + " ELEMENTS " + elements);
subList.add(s);
System.out.println("2 STRING S " + s + " TO SUBLIST " + subList);
}
}
return subList;
}