int count = 0;
for (int i = 0; i < width - 1; i++)
{
for(int j = 0; j < height - 1; j++)
{
new HashSet();
count++;
}
}
我在两个for循环中创建集合,但是如何获取对集合的引用?我怎么能打电话给&#34; set1&#34;或&#34; set2&#34;例如?
答案 0 :(得分:3)
您可以尝试将它们放在Arraylist
。
int count = 0;
List<Set<YourClass>> arr = new ArrayList<Set<YourClass>>();
for (int i = 0; i < width - 1; i++)
{
for(int j = 0; j < height - 1; j++)
{
Set s = new HashSet<YourClass>();
arr.add(s);
count++;
}
}
然后你可以打电话给你想要的任何一个:
Set theFirstSet = arr.get(0);
与他们一起做事:
theFirstSet.add(your_class_instance);
答案 1 :(得分:0)
这些集需要一个名称,例如
HashSet set1;
在类级别创建它们,以便可以在任何地方引用。
set1 = new HashSet();