python中的LinkedHashSet替代方案

时间:2021-01-07 17:07:19

标签: java python-3.x linkedhashmap linkedhashset

Iterator i1=h1.iterator();
while(i1.hasNext()){
int p=(int)(i1.next());
}

请用python编程语言术语解释这段代码,这里的h1是linkedhashset 提前致谢

1 个答案:

答案 0 :(得分:1)

您使用有不同的方式来迭代“LinkedHashSet”,例如:“的forEach”或“迭代”

迭代器是可用于进行迭代的集合中的所有元素的对象。迭代器有两个重要的方法(hasNext,下一个)。

我对你的每一行代码进行了解释

Iterator i1 = h1.iterator(); //obtain an iterator of the collection
while(i1.hasNext()) { // Continue iterating because has a next element 
  int p=(int)(i1.next()); //return the next element and parse it to "int"
}

还与一个Iterator可以添加或删除元素

其他资源:

Java Iterator - Geeks for Geeks

Java official Documentation