linkedhashset替代方案

tag5nh1u  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(409)
  1. Iterator i1=h1.iterator();
  2. while(i1.hasNext()){
  3. int p=(int)(i1.next());
  4. }

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

pokxtpni

pokxtpni1#

迭代“linkedhashset”有不同的方法,例如使用:“foreach”或“iterate”。
迭代器是可用于迭代集合中所有元素的对象。迭代器有两个重要的方法(hasnext,next)。
我对你的每一行代码都做了解释

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

还可以使用迭代器添加或删除元素。
其他资源:
java迭代器-geeks for geeks
java官方文档

相关问题