题目描述:
给定两个字符串:s1和s2
s1:welcome to world
s2:come
要求在输出的结果中将s1中存在的s2的字符删除。
最终输出的结果:wl t wrld
这里将会用到数组来解决此问题。
首先,定义一个数组ArrayList(),其次将两个对比的字符串进行存储。
对字符串s1进行遍历,判断s1中的字符是否出现在s2中,如果不存在,则将字符存储到数组中。
完整代码展示:
public static void main(String[] args) {
//首先定义一个数组,因为这里是字符,所以用到的是character;
ArrayList<Character> list = new ArrayList<>();
//定义两个字符串,用于存储题目中给定的两个字符串
String s1 = "welcome to world";
String s2 = "come";
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
if(!s2.contains(ch+"")){
list.add(ch);
}
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i));
}
}
运行程序后的输出结果: