首页 > 解决方案 > 迭代器 toIndexedSequence 长度与插入器长度不匹配

问题描述

在迭代器上调用 toIndexedSequence 时遇到一个奇怪的错误。迭代器的长度为 81,但生成的 indexedSequence 的长度为 257。当我打印 indexedSequence 时,我得到两个向量。第一个是 81 的正确长度,但它打印的第二个是 257。我不确定它为什么会打印两个向量。第二个似乎是在我的元素之间添加零。

def apply(source: scala.io.Source): Sudoku = {
    val vec = for( x <- source.toVector if !isWhiteSpace(x) ) yield { if( x>='1' && x<='9') x - 48 else 0 }
    println( vec )
    new Sudoku(vec)
  }
  def isWhiteSpace( char: Char ):Boolean = char == (' ' | '\n')


ouput:
Vector(9, 0, 0, 0, 8, 3, 0, 1, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 4, 0, 0, 0, 0, 7, 0, 0, 6, 0, 5, 0, 0, 0, 1, 0, 4, 0, 3, 0, 0, 0, 6, 0, 9, 0, 0, 7, 0, 0, 0, 0, 9, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 4, 0, 5, 2, 0, 0, 0, 6)
Vector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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