【问题标题】:"Removing" Duplicate spaces from a char array java从字符数组java中“删除”重复空格
【发布时间】:2016-06-04 18:19:45
【问题描述】:

我正在尝试创建一个方法,该方法将采用一个 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';
    }
}

谢谢大家。

【问题讨论】:

  • 这对字符有效吗?最重要的是,数组?它似乎只适用于字符串
  • 您希望将所有空格移到末尾。对吗?
  • 正确,所有空格都将作为 \u0000 个字符出现在末尾​​span>
  • 您的问题解决了吗?

标签: java string for-loop


【解决方案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 uv
  • @PiyushGupta 你不应该删除那个。应该做出适当的更正。感谢您的支持!
【解决方案2】:

如果您想保留当前代码,非常简单的解决方案。 只需添加 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';
    }
}

【讨论】:

    【解决方案3】:
        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;
    

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 2023-03-29
      • 2021-02-02
      • 2013-09-06
      • 2013-04-14
      相关资源
      最近更新 更多