2010-11-29 77 views
2

我有一個返回Sets集的方法。 以下是我的意思是:該方法返回集合Java編程幫助

public static HashSet methodName(){ 
HashSet c= new HashSet(); 

c.add(x); //x is a HashSet of number 
c.add(y); //y is a Hashset of numbers 

return c; 
} 

後,我將其輸入到一個ArrayList

ArrayList<HashSet> xxx= new ArrayList<Hashset>(); 
y=methodName(); 
xxx.add(y); 

該方法被調用幾次,每次我進入套的套入時間陣列列表。

我的問題是。現在我想通過arraylist並找到包含最少數量集合的集合。我怎麼做?非常感謝提前。任何幫助將不勝感激。

回答

0

一個簡單的方法來通過一個數組是通過Iterator。這些在foreach循環中使用,可以在知道數組中使用的元素類型(通過泛型)時使用。你可以在你的外部使用HashSet - 包含ArrayList

for (HashSet set : xxx) { 
    // you need to iterate over the elements in your HashSet here and determine which internal Set has the most elements 
    for (Iterator iter = set.iterator(); iter.hasNext();) { 
     HashSet innerSet = (HashSet) iter.next(); 
     // do the size test 
    } 

} 
+0

你能告訴我整個代碼嗎?你會如何比較尺寸 – sap 2010-11-29 04:05:44