題目:洗掉公共字符【牛客網】
題目要求:
輸入兩個字串,從第一字串中洗掉第二個字串中包含的所有字符,例如,輸入”They are students.”和”aeiou”,則洗掉之后的第一個字串變成”Thy r stdnts.”
輸入描述:
每個測驗輸入包含2個字串
例如:
輸入:
They are students.
aeiou
輸出:
Thy r stdnts.
主要思想:
使用一個for回圈遍歷第一個字串的每一個元素,如果元素不包含在第二個字串中,就把此元素放在一個順序表中,輸出順序表的每一個元素
源代碼:
import java.util.*;
public class Main {
public static void main1(String[] args) {
Scanner scanner = new Scanner(System.in);
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
ArrayList<Character> list = new ArrayList<>();
for (int i = 0;i < str1.length();i++){
if (!str2.contains(str1.charAt(i)+"")){
list.add(str1.charAt(i));
}
}
for (int j = 0;j < list.size();j++){
System.out.print(list.get(j));
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/292706.html
標籤:java
上一篇:JavaSE知識總結(1)
