代码之家  ›  专栏  ›  技术社区  ›  Orest Dymarchuk

使用另一个字符串数组Java从字符串数组中删除元素

  •  0
  • Orest Dymarchuk  · 技术社区  · 2 年前

    我试图使用字符串B的数组从字符串A中删除元素。 你能告诉我我做错了什么吗?

    class Test {
        public static void main(String[] args) {
            WordDeleter wordDeleter = new WordDeleter();
    
            // Hello
            System.out.println(wordDeleter.remove("Hello Java", new String[] { "Java" }));
    
            // The Athens in
            System.out.println(wordDeleter.remove("The Athens is in Greece", new String[] { "is", "Greece" }));
    
            // This cat
            System.out.println(wordDeleter.remove("This is cat", new String[] { "is" }));
        }
    }
    
    class WordDeleter {
        public String remove(String phrase, String[] words) {       
            String[] arrayPhrase = phrase.split(" ");       
            String result = "";
            
            for (int i = 0; i < arrayPhrase.length; i++) {
                for (int j = 0; j < words.length; j++) {
                    if (!arrayPhrase[i].equalsIgnoreCase(words[j]))
                        result += arrayPhrase[i] + " ";
                }
            }
            return result.trim();
        }
    }
    

    Hello
    The The Athens Athens is in in Greece
    This cat
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   Shavk with a Hoon    2 年前

    你很接近,但你需要使用有用的 .contains() for loop 检查两个单词,因此复制。此外,不允许的单词只写了一次,因此它实际上“起作用”,但不是你想要的方式。

    class Test {
        public static void main(String[] args) {
            WordDeleter wordDeleter = new WordDeleter();
    
            // Hello
            System.out.println(wordDeleter.remove("Hello Java", new String[] { "Java" }));
    
            // The Athens in
            System.out.println(wordDeleter.remove("The Athens is in Greece", new String[] { "is", "Greece" }));
    
            // This cat
            System.out.println(wordDeleter.remove("This is cat", new String[] { "is" }));
        }
    }
    
    class WordDeleter {
        public String remove(String phrase, String[] words) {       
            String[] arrayPhrase = phrase.split(" ");       
            String result = "";
            
            for (int i = 0; i < arrayPhrase.length; i++) {
                // If the word is not contained in the not allowed array
                if(!words.contains(arrayPhrase[i])){
                           result += arrayPhrase[i] + " ";
                }
            }
            return result.trim();
        }
    }
    

    你可以做的另一件事是使用 StringBuilder String result = "" StringBuilder result = new StringBuilder() . 而不是 result +=arrayPhrase[i] + " "; 使用 result.append(arrayPhrase[i] + " "); 因此:

    class Test {
        public static void main(String[] args) {
            WordDeleter wordDeleter = new WordDeleter();
    
            // Hello
            System.out.println(wordDeleter.remove("Hello Java", new String[] { "Java" }));
    
            // The Athens in
            System.out.println(wordDeleter.remove("The Athens is in Greece", new String[] { "is", "Greece" }));
    
            // This cat
            System.out.println(wordDeleter.remove("This is cat", new String[] { "is" }));
        }
    }
    
    class WordDeleter {
        public String remove(String phrase, String[] words) {       
            String[] arrayPhrase = phrase.split(" ");       
            StringBuilder result = new StringBuilder();
            
            for (int i = 0; i < arrayPhrase.length; i++) {
                // If the word is not contained in the not allowed array
                if(!words.contains(arrayPhrase[i])){
                          result.append(arrayPhrase[i] + " ");
                }
            }
            return result.toString().trim();
        }
    }
    
        2
  •  0
  •   Alexander Ivanchenko    2 年前

    大堆 给定字符串 是生成一个 HashSet 从给定 并检查 ,是否存在于 设置

    为了避免字符串连接的开销,我们可以使用 StringJoiner

    这就是它的实现方式。

    public static String remove(String phrase, String[] words) {
        String[] arrayPhrase = phrase.split(" ");
        Set<String> wordsToRemove = Arrays.stream(words).collect(Collectors.toSet());
        StringJoiner result = new StringJoiner(" ");
        
        for (String word: arrayPhrase) {
            if (!wordsToRemove.contains(word)) {
                result.add(word);
            }
        }
        
        return result.toString();
    }