JAVA 编程 找出两个字符串中所有共同的字符
展开全部
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* 找出两个字符串中所有共同的字符
*/
public class ExistChars {
public static void main(String[] args){
String s1 = "accddaolaf";
String s2 = "bdefmcgambbbbb";
int m = s1.length();
int n = s2.length();
Set<Character> st = new LinkedHashSet<Character>();
for(int i=0; i<m; i++){
char c1 = s1.charAt(i);
for(int j=0; j<n; j++){
char c2 = s2.charAt(j);
if(c2 == c1){
st.add(c1);
break;
}
}
}
Iterator<Character> it = st.iterator();
while(it.hasNext()){
System.out.print(it.next());
}
}
}
import java.util.LinkedHashSet;
import java.util.Set;
/**
* 找出两个字符串中所有共同的字符
*/
public class ExistChars {
public static void main(String[] args){
String s1 = "accddaolaf";
String s2 = "bdefmcgambbbbb";
int m = s1.length();
int n = s2.length();
Set<Character> st = new LinkedHashSet<Character>();
for(int i=0; i<m; i++){
char c1 = s1.charAt(i);
for(int j=0; j<n; j++){
char c2 = s2.charAt(j);
if(c2 == c1){
st.add(c1);
break;
}
}
}
Iterator<Character> it = st.iterator();
while(it.hasNext()){
System.out.print(it.next());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询