CVTE面试题:“删除字符串一中出现的所有字符串二的字符”解析

16 阅读1分钟

题目

CVTE面试题:删除字符串1当中出现的所有字符串2当中的字符。该题可以运用ArrayList进行解决, 下面给出我的思路图解,代码图解,完整代码

图解

删除字符串1当中所有出现字符串2的字符.png

完整代码

//题目:删除字符串一中,出现的所有字符串2当中的字符
import java.util.*;
import java.util.List;

public static List<Character> func(String str1, String str2) {
    List<Character> List = new ArrayList<>();//创建一个空列表
    for (int i = 0; i < str1.length(); i++) {//for循环遍历str1
        char ch = str1.charAt(i);
        if (!str2.contains(ch+"")) {//判断str2中没有的字符
            List.add(ch);//没有的字符全部丢进List中
        }
    }
    return List;
}

public static void main(String[] args) {
    List<Character> ret =func("welcome to cvte","come");//传值给func方法
    for (char ch: ret) {//循环打印可以消去[]括号
        System.out.print(ch);
    }
}