我的代码使用java.util.NoSuchElementException崩溃:接下来是空的迭代器异常。
def myfunction(arr : Array[(Int,(String,Int))]) = {
val values = (arr.sortBy(x => (-x._2._2, x._2._1.head)).toList)
...........................
代码在我试图对数组进行排序的第一行中崩溃。
var arr = Array((1,("kk",1)),(1,("hh",1)),(1,("jj",3)),(1,("pp",3)))
我正在尝试根据内部元组的第二个元素对数组进行排序。如果相等,则排序应在内部元组的第一个元素上进行。
输出-((1,(“pp”,3)),(1,(“jj”,3)),(1,(“hh”,1)),(1,(“kk”,1)) )
在某些情况下这会崩溃(通常可以正常工作),我猜这是由于空数组造成的。
我如何摆脱这种崩溃或其他任何达到相同结果的优雅方式。
最佳答案
发生这种情况是因为您的一个数组项(Int,(String,Int))
包含空字符串。
"".head
导致
java.util.NoSuchElementException: next on empty iterator
使用
x._2._1.headOption
关于Scala代码使用java.util.NoSuchElementException : next on empty iterator崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28274165/