Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何逐字符查找字符串的所有组合_Java - Fatal编程技术网

Java 如何逐字符查找字符串的所有组合

Java 如何逐字符查找字符串的所有组合,java,Java,例如,如果我有 猫 在JAVA中,我希望所有可能的字符组合一次更改一个字符(不包括组合CAT): 到目前为止,我有: public class alphaTesting { public static void main(String[] args) throws Exception { char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray(); for(int i = 0 ; i

例如,如果我有

在JAVA中,我希望所有可能的字符组合一次更改一个字符(不包括组合CAT):

到目前为止,我有:

public class alphaTesting
{
   public static void main(String[] args) throws Exception 
   {    
      char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();    

    for(int i = 0 ; i < alphabet.length ; i++)
    {
        System.out.println(alphabet[i] + "\n");
    }

    String word = "cat";

    for(int i = 0 ; i < word.length() ; i++)
    {
        for(int j = 0 ; j < alphabet.length ; j++)
        {
            // System.out.println(alphabet[i] + "\n");
            // System.out.println(charAt(i) = )
            word = word.replace(word.charAt(i), alphabet[j]);
            System.out.println(word);
        }       
    }

   }
}

但是,在进入word.charAt(1)之前,内部循环不是应该首先运行所有字母吗?

如果您喜欢更优雅的代码,下面的代码应该可以完成这项工作。 我在这里使用了一个名为
vavr
的函数库。 您可能希望将其添加到依赖项中

import io.vavr.collection.Stream;
import io.vavr.collection.Traversable;

public class Main {
    public static void main(String[] args) {
        Stream.ofAll("abcdefghijklmnopqrstuvwxyz".toCharArray())
                .map(Character::toUpperCase)
                .combinations(3)
                .map(Traversable::mkString)
                .filter(s -> !s.equals("CAT"))
                .take(100)
                .forEach(System.out::println);
    }
}
take(100)
只获取前100个值。您可以更改它以获取任意多的值,也可以删除它以获取所有值

注意:这是JAVA-8

这很有效

字符串word2=“”

for(int i=0;i
您忘了发布自己的解决方案尝试,并用它来提出更具体的问题。请浏览、和部分,了解此网站的工作原理,并帮助您改进当前和未来的问题,这可以帮助您获得更好的答案。请同时查看。我道歉。我添加了我的尝试编号te该方法
replace()
类中的
String
替换所有出现的字符。如果单词多次包含同一字符,则不适用。最好使用class
StringBuilder
。例如,它有方法。但请注意
String
是不可变的,
StringBuilder
不是。String word2=word.replace(word.charAt(i) ,字母表[j]);这起作用了
aat
bbt
cct
ddt
eet
fft
ggt
import io.vavr.collection.Stream;
import io.vavr.collection.Traversable;

public class Main {
    public static void main(String[] args) {
        Stream.ofAll("abcdefghijklmnopqrstuvwxyz".toCharArray())
                .map(Character::toUpperCase)
                .combinations(3)
                .map(Traversable::mkString)
                .filter(s -> !s.equals("CAT"))
                .take(100)
                .forEach(System.out::println);
    }
}
 for(int i = 0 ; i < word.length() ; i++)
            {
                for(int j = 0 ; j < alphabet.length ; j++)
                {
                    // show indices
                    System.out.println("i = " + i + " j = " + j);           
                    word2 = word.substring(0,i) + alphabet[j] + word.substring(i+1); ;
                            if (!word2.equals(word) {
                            System.out.println(word2);
                            }
                }

            }