"删除" char数组java中的重复空格

时间:2016-02-23 04:05:08

标签: java string for-loop

我尝试创建一个方法,该方法将获取一个char数组,删除任何重复的空格(2个或更多),然后在末尾放置'\u0000'个字符,因为切掉了很多空格所以数组长度满足。我意识到我必须将元素向下移动,但这是我遇到麻烦的地方。我的程序可以正常工作2个空格,但连续三个序列会将其抛弃。我理解为什么会这样,但我不知道如何解决它。我知道它来自代码characters[j] = characters[j+1],但我不知道如何修复它。

int duplicateCount = 0;
// Create a loop to read the element values
for(int i = 0; i + 1 < characters.length; i++){
    // If the element is a space and the next one is a space
    if(characters[i] == ' ' && characters[i+1] == ' '){
        // Add to duplicate count and start shifting values down
        duplicateCount++;
        // *THIS IS WHERE I THINK BUG IS*
        for(int j = i; j < characters.length - 1; j++){
            characters[j] = characters[j+1];
            }
        }
    }
    // Replace characters at end with how many duplicates were found
    for(int replace = characters.length - duplicateCount; replace < characters.length; replace++){
        characters[replace] = '\u0000';
    }
}

谢谢大家。

3 个答案:

答案 0 :(得分:1)

根据我的理解,您希望从非空格字符之间删除所有空格,并将\ u0000添加到结尾。

如果这是问题,请尝试以下方法: 我使用loopsif语句来实现它。

for (int i = 0; i < characters.length; i++) {
        int j =i+1;
        if (characters[i] == ' ' || characters[i] == '\u0000' ) {
            while (j<characters.length && (characters[j] == ' ' || characters[j] == '\u0000')) {

                j++;  //increment j till a non-space char is found

            }
            if(j<characters.length && (characters[j] != ' ' || characters[j] != '\u0000')) 
                // to ensure that the control entered while
            {
           characters[i] = characters[j];   //swapping the values
            characters[j] = '\u0000';    //giving value \u0000 to position j
        }
        }

    }

答案 1 :(得分:0)

如果您想保留当前代码,这是非常简单的解决方案。 只需添加1行i - 。

int duplicateCount = 0;
// Create a loop to read the element values
for(int i = 0; i + 1 < characters.length; i++){
    // If the element is a space and the next one is a space
    if(characters[i] == ' ' && characters[i+1] == ' '){
        // Add to duplicate count and start shifting values down
        duplicateCount++;
        // *THIS IS WHERE I THINK BUG IS*
        for(int j = i; j < characters.length - 1; j++){
            characters[j] = characters[j+1];
            }
         i--; // so that multiple space case can be handled
        }
    }
    // Replace characters at end with how many duplicates were found
    for(int replace = characters.length - duplicateCount; replace < characters.length; replace++){
        characters[replace] = '\u0000';
    }
}

答案 2 :(得分:0)

    int count = 0;  // Count of non-space elements

    // Traverse the array. If element encountered is
    // non-space, then replace the element at index 'count'
    // with this element
    for (int i = 0; i < n; i++)
        if (arr[i] != '')
            arr[count++] = arr[i]; // here count is
                                   // incremented

    // Now all non-space elements have been shifted to
    // Make all elements '\u0000' from count to end.
    while (count < n)
        arr[count++] = 0;