CustomString类remove方法返回重复字符串问题求助
Java CustomString类remove方法实现问题
需求说明
作业要求为CustomString类实现remove方法,核心规则(以官方Javadoc为准):
- 接收字符串参数
arg,返回当前myString的新字符串版本,仅移除arg中的字母字符(字母匹配不区分大小写) - 所有非字母字符不受影响(即使arg包含数字、符号等,也不会移除原字符串中的对应字符)
- 若
myString为null、空或未被设置,返回空字符串
用户自行描述的需求存在偏差(认为要移除arg中的所有字符),需以Javadoc及示例为准。
现有代码问题
- 循环逻辑错误:外层遍历
arg字符,内层遍历myString字符,导致符合保留条件的字符被重复添加(每处理一个arg字符就追加一次) - 需求理解偏差:错误处理了arg中的数字字符,尝试移除原字符串对应数字,但Javadoc明确仅移除字母
- 非字母字符丢失:代码中跳过了非字母非数字的字符(如
@、,),未将其加入结果
现有代码片段
构造函数
public CustomString() { // TODO Implement constructor this.myString = null; this.isSet = false; }
remove方法及Javadoc
/** * Returns a new string version of the current string where the alphabetical characters specified in the given arg, are removed. * * The alphabetical characters to be removed are case insensitive. * All non-alphabetical characters are unaffected. * If the current string is null, empty, or has not been set to a value, this method should return an empty string. * * Example(s): * - For a current string "my lucky numbers are 6, 8, and 19.", calling remove("ra6") would return "my lucky numbes e 6, 8, nd 19.". * - For a current string "my lucky numbers are 6, 8, and 19.", calling remove("6,.") would return "my lucky numbers are 6, 8, and 19.". * - For a current string "my lucky numbers are 6, 8, and 19.", calling remove("") would return "my lucky numbers are 6, 8, and 19.". * * Remember: This method builds and returns a new string, and does not directly modify the myString field. * * @param arg the string containing the alphabetical characters to be removed from the current string * @return new string in which the alphabetical characters specified in the arg are removed */ public String remove(String arg) { // TODO Implement method String newString; char[] myStrArr = myString.toCharArray(); char[] argArr = arg.toCharArray(); String newStr = ""; //a,1 and za,,231 for (int i = 0; i < argArr.length; i++) { for (int x = 0; x < myStrArr.length; x++) { if(Character.isLetter(argArr[i]) && Character.isLetter(myStrArr[x])) { if ((Character.toUpperCase(argArr[i]) == myStrArr[x]) || (Character.toLowerCase(argArr[i]) == myStrArr[x])) { continue; } else { String newChar = Character.toString(myStrArr[x]); newStr += newChar; } } else if (Character.isDigit(argArr[i]) && Character.isDigit(myStrArr[x])) { if(argArr[i] == myStrArr[x]) { continue; } else { String newChar = Character.toString(myStrArr[x]); newStr += newChar; } } else { continue; } } } return newStr; }
测试情况
- 当前字符串:
abc@,123 - 调用
remove("a3,")实际返回:bc12 - 按Javadoc需求的正确预期:
bc@,123 - 若按用户自行描述的“移除arg中所有字符”需求,预期为:
bc@,12
修复方案
方案1:符合Javadoc官方需求
import java.util.HashSet; import java.util.Set; public String remove(String arg) { // 处理边界情况 if (myString == null || myString.isEmpty() || !isSet) { return ""; } // 提取arg中的字母字符,统一转大写存入集合,实现大小写不敏感判断 Set<Character> lettersToRemove = new HashSet<>(); for (char c : arg.toCharArray()) { if (Character.isLetter(c)) { lettersToRemove.add(Character.toUpperCase(c)); } } StringBuilder result = new StringBuilder(); for (char c : myString.toCharArray()) { // 字母判断是否需要移除,非字母直接保留 if (Character.isLetter(c)) { if (!lettersToRemove.contains(Character.toUpperCase(c))) { result.append(c); } } else { result.append(c); } } return result.toString(); }
代码说明
- 边界处理:严格遵循需求,避免空指针异常
- 高效判断:用HashSet存储需移除的字母,O(1)时间复杂度判断,性能更优
- 正确遍历:仅遍历原字符串一次,避免重复添加问题
- 非字母保留:所有非字母字符直接加入结果,符合Javadoc规则
- 性能优化:用StringBuilder替代字符串拼接,避免频繁创建字符串对象
方案2:符合用户自行描述的“移除arg中所有字符”需求
若实际需求是移除arg中的所有字符(包括数字、符号),可使用以下版本:
import java.util.HashSet; import java.util.Set; public String remove(String arg) { if (myString == null || myString.isEmpty() || !isSet) { return ""; } Set<Character> charsToRemove = new HashSet<>(); for (char c : arg.toCharArray()) { // 字母统一转大写,实现大小写不敏感匹配 if (Character.isLetter(c)) { charsToRemove.add(Character.toUpperCase(c)); } else { charsToRemove.add(c); } } StringBuilder result = new StringBuilder(); for (char c : myString.toCharArray()) { if (Character.isLetter(c)) { if (!charsToRemove.contains(Character.toUpperCase(c))) { result.append(c); } } else { if (!charsToRemove.contains(c)) { result.append(c); } } } return result.toString(); }
此版本会移除arg中所有类型的字符,字母匹配仍不区分大小写,测试用例abc@,123调用remove("a3,")会返回预期的bc@,12。
内容的提问来源于stack exchange,提问作者sirrene
相关产品推荐
相关产品推荐

