2010-11-28 72 views

回答

2

ts1在這些操作之後會有「d」。

String str1[] = {"a","b","aa","c"};
String str2[] = {"aa","a","b","c","d"};
TreeSet ts1 = new TreeSet(Arrays.asList(str2));
ts1.removeAll(Arrays.asList(str1));

0

轉換陣列到一個列表,並將其添加到一套會自動刪除重複。使用Set接口的toArray()方法將元素作爲數組獲取。

String[] str1 = {"a","b","aa","c"}; 
    String[] str2 = {"aa","a","b","c","d"}; 

    Set set = new HashSet(); 
    set.addAll(Arrays.asList(str1));//add first array to set, removes duplicates 
    set.addAll(Arrays.asList(str2));//add second array to set, removes duplicates 
    String[] str3 = (String[])set.toArray(new String[set.size()]);//convert back to array