Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.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 避免循环内ToCharray()或arrayCopy的冗余_Java_Arrays_Time Complexity - Fatal编程技术网

Java 避免循环内ToCharray()或arrayCopy的冗余

Java 避免循环内ToCharray()或arrayCopy的冗余,java,arrays,time-complexity,Java,Arrays,Time Complexity,我采用了以下方法,用字母表中所有可能的字母替换单词中的每个字母,并将所有备选单词存储在一个数组中: private String[] replaceLetters(String word, char[] alphabet){ //replacing one letter with an arbitrary letter from alphabet, for each letter place in the word. int wordLength = word.length();

我采用了以下方法,用字母表中所有可能的字母替换单词中的每个字母,并将所有备选单词存储在一个数组中:

private String[] replaceLetters(String word, char[] alphabet){
    //replacing one letter with an arbitrary letter from alphabet, for each letter place in the word.
    int wordLength = word.length();
    String[] words = new String[alphabet.length*wordLength];
    char[] tmpWord = word.toCharArray();
    int counter = 0;
    for(int i = 0; i<wordLength; i++){
        tmpWord = word.toCharArray();   
        for(char c : alphabet){
            tmpWord[i] = c;
            words[counter] = new String(tmpWord);
            counter++;
        }
    }
    return words;
}
私有字符串[]替换字母(字符串字、字符[]字母表){
//将单词中的每个字母替换为字母表中的任意字母。
int wordLength=word.length();
String[]words=新字符串[alphabet.length*wordLength];
char[]tmpWord=word.toCharArray();
int计数器=0;

对于(int i=0;i您可以在外部循环之前创建一次
tmpWord
,并在创建新的
字符串后将更改的字符放回内部
for
,将
tmpWord
还原为其原始内容


不确定“无摩擦”的程度。

您可以在外部循环之前创建一次
tmpWord
,并在创建新的
字符串后将更改的字符放回内部
for
循环,将
tmpWord
还原为其原始内容


不确定“无摩擦”的程度。

新字符串(tmpWord)
也会复制数组。但我仍然需要将其转换为慈善数组,因此我不确定它是否节省时间?
新字符串(tmpWord)
也会复制数组。但我仍然需要将其转换为慈善数组,因此我不确定它是否能节省时间?您可能希望将原始字符存储在一个变量中以便于放回,但这正是我要给出的答案。是的!这正是我无法想到的。此解决方案是显而易见的s和性能更好。谢谢你们!你们可能想将原始字符存储在一个变量中,以便于放回,但这正是我要给出的答案。是的!这正是我想不到的。这个解决方案很明显,性能也更好。谢谢你们!