假設我有兩個Arraylists。Java的retainAll()不工作?
a.add("Isabella");
a.add("Angelina");
a.add("Pille");
a.add("Hazem");
b.add("Isabella");
b.add("Angelina");
b.add("Bianca");
a.retainAll(b);
這應該給我Arraylist a與以下元素:Isabella, Angelina, Pille, Hazem
。但是,當我嘗試a.size()
我得到0.爲什麼?
我的輸出:
[DEBUG] The Karate Kid
[DEBUG] The Day the Earth Stood Still
[DEBUG] The Pursuit of Happyness
[DEBUG] Justin Bieber: Never Say Never
[DEBUG] After Earth
[DEBUG] Independence Day
[DEBUG] Men in Black
[DEBUG] Men in Black II
[DEBUG] Hancock
[DEBUG] Shark Tale
[DEBUG] Made in America
[DEBUG] Six Degrees of Separation
[DEBUG] Jersey Girl
[DEBUG] The Legend of Bagger Vance
[DEBUG] Men in Black 3
[DEBUG] Seven Pounds
[DEBUG] Bad Boys II
[DEBUG] Bad Boys 3
[DEBUG] Enemy of the State
[DEBUG] Wild Wild West
[DEBUG] Hitch
[DEBUG] Ali
[DEBUG] I, Robot
[DEBUG] Live 8
[DEBUG] Where The Day Takes You
[DEBUG] Independence Day 3
[DEBUG] I, Robot 2
[DEBUG] The Pursuit of Happyness
[DEBUG] I Am Legend
[DEBUG] Independence Day 2
[DEBUG] After Earth
[DEBUG] Bad Boys
[DEBUG] Partners in Time: The Making of MIB 3
[DEBUG] David Blaine: Real or Magic
[DEBUG] Size: 0
,第一部分是影片主演賈登·史密斯,二是電影的主演威爾·史密斯,我只希望有兩部影片。 retainAll()
是這種工作的最佳方法嗎?
你讀過零售店的javadoc嗎? –
'只保留包含在指定集合中的列表中的元素。換句話說,從這個列表中刪除所有未包含在指定集合中的元素。「正是我想要的,這有什麼問題? – JChris
我試了一下名字。我無法重現這一點。你確定沒有其他干擾嗎? – MAV