val files = new File("data").list()
val filtered = files.filter(name => name.contains("txn"))
val masterList = new ListBuffer[String]
for (file <- filtered) {
val lines = Source.fromFile(new File("data\\" + file), "UTF-16").getLines
val cleaned = lines.filter(!masterList.contains(_))
println("*" + cleaned.length)
cleaned.foreach(println(_))
println("**" + cleaned.length)
cleaned.foreach(masterList.append(_))
}
代码的输出如下
*175
**0
我不明白为什么我的名单会消失
答案 0 :(得分:19)
cleaned
是一个迭代器。
scala> val cleaned = lines.filter(_!="")
cleaned: Iterator[String] = non-empty iterator
分配后,它是非空的。 scala中的迭代器是单独使用的 - 一旦你遍历它(例如通过应用length
方法)它就变空了:
scala> cleaned.length
res0: Int = 13
scala> cleaned.length
res1: Int = 0
您可以通过转换为List或Seq(lazy)来修复该行为:
scala> val lines=Source.fromFile("bla.txt").getLines
lines: Iterator[String] = non-empty iterator
scala> val cleaned = lines.filter(_!="").toSeq
cleaned: Seq[String] = Stream(first, ?)
scala> cleaned.length
res4: Int = 13
scala> cleaned.length
res5: Int = 13
答案 1 :(得分:6)
Source.fromFile(new File("data\\" + file), "UTF-16").getLines
会返回Iterator[String]
。过滤仍会返回Iterator
。在迭代器上调用length
将导致它被完全读取,直到hasNext
返回false。第二次,它是空的。类似于:
scala> val cleaned = List(1,2).iterator
cleaned: Iterator[Int] = non-empty iterator
scala> cleaned.length
res10: Int = 2
scala> cleaned.length
res11: Int = 0