简化和压缩阵列上的多个编辑操作。爪哇岛



我有一些原始输出,我想要清理并使其美观,但现在我以一种非常丑陋和繁琐的方式进行,我想知道是否有人可能知道一种干净而优雅的方式来执行相同的操作。

    int size = charOutput.size();
    for (int i = size - 1; i >= 1; i--) 
    {
        if(charOutput.get(i).compareTo(charOutput.get(i - 1)) == 0) 
        {
            charOutput.remove(i);
        }
    }

    for(int x = 0; x < charOutput.size(); x++)
    {
        if(charOutput.get(x) == '?') 
        {
            charOutput.remove(x);
        }
    }

    String firstOne = Arrays.toString(charOutput.toArray());
    String secondOne = firstOne.replaceAll(",","");
    String thirdOne = secondOne.substring(1, secondOne.length() - 1);
    String output = thirdOne.replaceAll(" ","");

    return output;

ZouZou有正确的代码来修复代码中的最后几个调用。我对for回路有一些建议。我希望我说对了…

当你用ZouZou建议的方法得到由charOutput表示的String后,这些工作。

您的第一个块似乎删除了所有重复的字母。您可以使用正则表达式:

Pattern removeRepeats = Pattern.compile("(.)\1{1,}");
// "(.)" creates a group that matches any character and puts it into a group
// "\1" gets converted to "1" which is a reference to the first group, i.e. the character that "(.)" matched
// "{1,}" means "one or more"
// So the overall effect is "one or more of a single character"
使用:

removeRepeats.matcher(s).replaceAll("$1");
// This creates a Matcher that matches the regex represented by removeRepeats to the contents of s, and replaces the parts of s that match the regex represented by removeRepeats with "$1", which is a reference to the first group captured (i.e. "(.)", which is the first character matched"

要删除问号,只需执行

Pattern removeQuestionMarks = Pattern.compile("\?");
// Because "?" is a special symbol in regex, you have to escape it with a backslash
// But since backslashes are also a special symbol, you have to escape the backslash too.

然后使用,做同样的事情,除了replaceAll("");

你完成了!

如果你真的想,你可以把很多正则表达式组合成两个超级正则表达式(和一个普通正则表达式):

Pattern p0 = Pattern.compile("(\[|\]|\,| )"); // removes brackets, commas, and spaces
Pattern p1 = Pattern.compile("(.)\1{1,}"); // Removes duplicate characters
Pattern p2 = Pattern.compile("\?");
String removeArrayCharacters = p0.matcher(charOutput.toString()).replaceAll("");
String removeDuplicates = p1.matcher(removeArrayCharacters).replaceAll("$1");
return p2.matcher(removeDuplicates).replaceAll("");

使用StringBuilder并附加您想要的每个字符,最后仅返回myBuilder.toString();

而不是:

String firstOne = Arrays.toString(charOutput.toArray());
String secondOne = firstOne.replaceAll(",","");
String thirdOne = secondOne.substring(1, secondOne.length() - 1);
String output = thirdOne.replaceAll(" ","");
return output;

只是做:

StringBuilder sb = new StringBuilder();
for(Character c : charOutput){
    sb.append(c);
}
return sb.toString();

注意,您正在做许多不必要的工作(通过遍历列表并删除一些元素)。你实际上可以做的就是迭代一次,然后如果条件满足你的要求(相邻的两个字符不相同,没有问号),然后将其直接附加到StringBuilder

此任务也可以是正则表达式的作业。

如果您不想使用正则表达式,请尝试此版本删除连续字符和'?':

int size = charOutput.size();
if (size == 1) return Character.toString((Character)charOutput.get(0));
else if (size == 0) return null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size - 1; i++) {
    Character temp = (Character)charOutput.get(i);
    if (!temp.equals(charOutput.get(i+1)) && !temp.equals('?'))
        sb.append(temp);
}
//for the last element
if (!charOutput.get(size-1).equals(charOutput.get(size-2)) 
        && !charOutput.get(size-1).equals('?'))
    sb.append(charOutput.get(size-1));
return sb.toString();

最新更新