gpt4 book ai didi

java - 如何获取对循环内部创建的集合的引用?

转载 作者:行者123 更新时间:2023-11-30 08:01:19 26 4
gpt4 key购买 nike

    int count = 0;
for (int i = 0; i < width - 1; i++)
{
for(int j = 0; j < height - 1; j++)
{
new HashSet();
count++;
}
}

我在两个 for 循环中创建集合,但如何获取对集合的引用?例如,我如何才能调用“set1”或“set2”?

最佳答案

您可以尝试将它们放在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);

关于java - 如何获取对循环内部创建的集合的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37781211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com