ファイル処理のためのScala cook bookを使い、コードの下に来ました。 IDEで実行しようとしましたが、エラーが発生しました。私は何かが欠けている、私はアレイの前にそのような構文に遭遇したことはありません。一般に配列解体構文
import java.io.IOException
import scala.io.{BufferedSource, Source}
object ReadingCSVfile extends App {
var bufferedSource = None: Option[BufferedSource]
try {
bufferedSource =
Some(
Source.fromFile(
"C:\\Users\\pswain\\IdeaProjects\\test1\\src\\main\\resources\\finance.csv")
)
for(i <- bufferedSource.get.getLines()) {
val Array(month, Income, Expenses, Profit) = i.split(",").map(x => x.trim)
println(s"$month $revenue $expenses $profit")
}
} catch {
case e : IOException => print(e.printStackTrace())
} finally {bufferedSource.get.close()}
}
この構文は、基本的に等号の左側に適用されるパターンマッチングです。配列は、左側の名前付きの値に分解されています。 – Samar