标签:java
/**
* 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)+" ");
}
}标签:java
原文地址:http://anglecode.blog.51cto.com/5628271/1619862