2010-11-29 182 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