作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 for 循环,如何遍历列表,并能够不遍历列表中的最后一个元素。
就我而言,我不想迭代第一个元素,而是需要向后迭代,这是我所拥有的:
for( thing <- things.reverse) {
//do some computation iterating over every element;
//break out of loop when first element is reached
}
最佳答案
您可以先删除第一项,然后再将其撤消:
for(thing <- things.drop(1).reverse) {
}
drop(1)
与
tail
相同:
for(thing <- things.tail.reverse) {
}
dropRight
:
for(thing <- things.reverse.dropRight(1)) {
}
init
:
for(thing <- things.reverse.init) {
}
关于Scala遍历列表,除了最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21565149/
我是一名优秀的程序员,十分优秀!