Can I iterate through a NodeList using for-each in Java?

前端 未结 10 1142
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 00:55

I want to iterate through a NodeList using a for-each loop in Java. I have it working with a for loop and a do-while loop but not for-each.

Node         


        
10条回答
  •  半阙折子戏
    2020-12-09 01:30

    Adding the happy little kotlin version for sience:

    fun NodeList.forEach(action: (Node) -> Unit) {
        (0 until this.length)
                .asSequence()
                .map { this.item(it) }
                .forEach { action(it) }
    }
    

    One can then use it with nodeList.forEach { do_something_awesome() }

提交回复
热议问题