/**
* 6-4
* 利用字符串类创建两个字符串对象,
* 找出两个字符串的所有相同字符
* 并将其输出在屏幕上
**/
import java.util.ArrayList; public class Test { public static void main (String[] args) { String str1 = "My name is Xingfei Du"; String str2 = "I like programming."; ArrayList<Character> array = new ArrayList<Character>(); /*找出相同所有的字符*/ for (int i = 0; i < str1.length(); i++) for (int j = 0; j < str2.length(); j++) if (str1.charAt(i) == str2.charAt(j)) array.add(str1.charAt(i)); /*筛选掉重复的字符*/ for (int l = 0; l < array.size(); l++) for (int m = 0; m < array.size(); m++) if (array.get(l)==array.get(m)) { array.remove(array.get(m)); } for (int i = 0; i < array.size(); i++) System.out.print(array.get(i)+" "); } }
时间: 2024-11-14 20:19:55