请你使用最高效的方法来实现
String[] s1 = {"a","b","c"};
String[] s2 = {"b","d","e"};
这两个字符串数组中哪些个字符相等,哪些个字符不相等

解决方案 »

  1.   

      咳嗽咳嗽 elquas的话 s1[1] == s2[0]  但是想用 if(s1[1] == s2[0]) 的话 那么这俩条数组全不相等 因为String 是对象  比如String s = "A";// s = "A";  //这里的S 已经不等于上面的S了。。  内存地址被改变了
      

  2.   

    s1 change to List
    foreach(String s : s2){
      if(list.contains(s)){
        //equals
      } else {
        //not equals
      }
    }我觉得还有更简便的方法。
      

  3.   

    equal是字符串相等,==是只相等!
      

  4.   

    public static void main(String[] args) {
    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);
    }
      

  5.   


    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));
    }
    }
    一个个的方法去掉注释测试,一次测试一个,不要把注释都去掉,结果自己看,其实方法多的是