java - Processing 是否支持迭代器?

标签 java iterator processing

我希望在Processing中使用一个hashmap,我希望使用一个迭代器来遍历hashmap中的所有条目。然而,当我希望使用迭代器时,却被告知“找不到名为 Iterator 的类或类型”。部分代码如下所示。

Iterator i = nodeTable.entrySet().iterator();  // Get an iterator
while (i.hasNext()) 
{
  Node nodeDisplay = (Node)i.next();
  nodeDisplay.drawSelf();
}

来自加工网站http://processing.org/reference/HashMap.html我知道迭代器可以用来遍历hashmap。但是,我找不到有关迭代器的更多信息。我想知道 Processing 是否支持迭代器?或者我应该导入一些库以便能够使用它们吗?

最佳答案

只要我解决了问题,我就会把我的部分代码放在这里,以防其他人遇到这个问题。再次感谢您的帮助。

import java.util.Iterator;  // Import the class of Iterator
// Class definition and the setup() function are omitted for simplicity

// The iterator is used here
HashMap<String, Node> nodeTable = new HashMap<String, Node>();
void draw(){
    // Part of this function is omitted
    Iterator<Node> i = nodeTable.values().iterator();
    // Here I use the iterator to get the nodes stored the hashtable and I use the function values() here. entrySet() or keySet() can also be used when necessary
    while (i.hasNext()) {
        Node nodeDisplay = (Node)i.next();
        // Now you can use the node from the hashmap
    }
}

关于java - Processing 是否支持迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004391/

相关文章:

java - 如何使用 for 循环使用键集迭代 HashMap

java - 使用 JSoup 修改内存中的 HTML

java - Java 中 HashMap 的迭代器

java - 当我悬停时如何使每个按钮突出显示

Java/P5 : How to declare an Array of Arraylists holding FloatLists?

java - DateTimeFormatter 自动更正无效(语法上可能)的日历日期

java - toString() 将 java hashmap 中的 ":"更改为 "="

c# - 在 C# 中实现双向枚举器

java - 查找 HashMap 中保存最小整数值的键

使用网络摄像头进行 Java 运动检测