我正在做以下操作。能夠將ArrayList <Set<String>>拋到ArrayList <String>沒有類拋出異常的字符串
Set<Set<String>> set1 = new HashSet<Set<String>>();
Set<Set<String>> set2 = new HashSet<Set<String>>();
set1.add(new HashSet<String>(Arrays.asList("s1","s2")));
set1.add(new HashSet<String>(Arrays.asList("s4","s5")));
set2.add(new HashSet<String>(Arrays.asList("s5","s4")));
ArrayList<String> ob = (ArrayList<String>)CollectionUtils.subtract(set1, set2);
ArrayList<Set<String>> ob1 = (ArrayList<Set<String>>)CollectionUtils.subtract(set1, set2);
System.out.println(ob);
System.out.println(ob1);
它給出以下輸出。
[S1,S2]
[S1,S2]
難道不應該給在第一種情況下的類轉換異常?
感謝,
山塔努
你的編譯器應該是給你一個警告,這是一個未經檢查的投(即,不能在運行時執行,因此可能會導致以後的'ClassCastException'鑄造的地方在那裏有沒有明確的投)。無論哪種方式,您都可以通過[Googling'unchecked cast'](https://www.google.com/search?q=unchecked+cast)找到大量信息(包括大量Stack Overflow問題)。 – ruakh 2013-03-12 06:32:51
嗨Ruakh,這兩種情況都給了我同樣的警告。 – shantanu 2013-03-12 06:35:27
所以,你應該谷歌搜索警告消息。或者,至少*,你應該在你的問題中包含警告信息! – ruakh 2013-03-12 06:37:43