请你使用最高效的方法来实现
String[] s1 = {"a","b","c"};
String[] s2 = {"b","d","e"};
这两个字符串数组中哪些个字符相等,哪些个字符不相等
String[] s1 = {"a","b","c"};
String[] s2 = {"b","d","e"};
这两个字符串数组中哪些个字符相等,哪些个字符不相等
解决方案 »
- 求助String转JSONObject问题
- 新手,求救,“”and null
- des解密问题:Given final block not properly padded
- 如何在jpanel的mouseClicked事件中,判断ctrl和shift键是否按下??
- setVisible,toFront但窗口不显示最前端
- 怎么做到的,这么强的软件??
- 已知文件里的数据是float类型的,怎么才可以将它们读出到一个FloatBuffer里?
- 极其简单的问题你一定能帮我找到毛病
- 请教
- 谁知道那里有NEC开发的WAtool的中文教程啊?(~~~或者谁用过啊~~)
- 帮忙看下关于事件的错误
- java 动态生成类
foreach(String s : s2){
if(list.contains(s)){
//equals
} else {
//not equals
}
}我觉得还有更简便的方法。
String[] s1 = {"a","b","c"};
String[] s2 = {"b","d","e"};
HashSet<String> ss1 = new HashSet<String>();
HashSet<String> diff;
HashSet<String> same = new HashSet<String>();
ss1.addAll(Arrays.asList(s1));
diff = (HashSet<String>) ss1.clone();
for (String s:s2){
if (ss1.contains(s)){
same.add(s);
diff.remove(s);
}
else
diff.add(s);
}
System.out.println(diff);
System.out.println(same);
}
public class Test { static String[] s1 = { "a", "b", "c" };
static String[] s2 = { "b", "d", "e" };
public static void method1() {
HashSet<String> ss1 = new HashSet<String>();
HashSet<String> same = new HashSet<String>();
for (String s : s2) {
if (ss1.contains(s)) {
same.add(s);
}
}
System.out.println(same);
} public static void method2(){
char[] charS1 = new String(s1[0]+s1[1]+s1[2]).toCharArray();
char[] charS2 = new String(s2[0]+s2[1]+s2[2]).toCharArray();
for(char ch:charS1){
for(char ch2:charS2){
if(ch == ch2){System.out.println(ch2);}
}
}
}
public static void method3(){
for(String str:s1){
for(String str2:s2){
if(str.equals(str2)){
System.out.println(str);
}
}
}
}
public static void method4(){
for(String str:s1){
for(String str2:s2){
if(str.hashCode()==str2.hashCode()){
System.out.println(str);
}
}
}
}
public static void main(String[] args) {
long start = System.nanoTime();
//method1();
//method2();
//method3();
//method4();
System.out.println("Time:"+(System.nanoTime()-start));
}
}
一个个的方法去掉注释测试,一次测试一个,不要把注释都去掉,结果自己看,其实方法多的是