Java 如何获取循环内部创建的集合的引用? int count=0; 对于(int i=0;i
我在两个for循环中创建集合,但是如何获得集合的引用呢?例如,如何调用“set1”或“set2”?您可以尝试将它们放入Java 如何获取循环内部创建的集合的引用? int count=0; 对于(int i=0;i,java,set,Java,Set,我在两个for循环中创建集合,但是如何获得集合的引用呢?例如,如何调用“set1”或“set2”?您可以尝试将它们放入数组列表中 int count = 0; for (int i = 0; i < width - 1; i++) { for(int j = 0; j < height - 1; j++) { new HashSet(); count++; }
数组列表中
int count = 0;
for (int i = 0; i < width - 1; i++)
{
for(int j = 0; j < height - 1; j++)
{
new HashSet();
count++;
}
}
和他们一起做事:
Set theFirstSet = arr.get(0);
集合需要一个名称,例如
theFirstSet.add(your_class_instance);
使它们成为类级别的字段,以便可以在任何地方引用
HashSet set1;
把它们放在一些集合中。我知道OP在他们的示例中使用它们,你真的不应该使用原始类型。没问题。我+1你。OP正在创建多个集合,而不仅仅是一个。我只是想把名字推到字段上,这样它们就可以在循环之外被引用。
HashSet set1;
set1 = new HashSet();