1
讀取字符串列表[INT]我有一個具有幾行這樣一個文本文件:如何從文件
SK1, 2, 3, 4, 5
SK2, 1, 3, 5, 1
我想讀它,並將其存儲在一個Map [字符串,列表[ Int]]但是我寫的代碼是以Map [String,List [Any]]的形式出現的,我不知道如何解決它。你可以幫我嗎?
def readFile(filename: String): Map[String, List[Any]] = {
var mapBuffer: Map[String, List[Any]] = Map()
try {
for (line <- Source.fromFile(filename).getLines()) {
val splitline = line.split(",").map(_.trim).toList
mapBuffer = mapBuffer ++ Map(splitline.head -> splitline.tail)
}
} catch {
case ex: Exception => println("There was an exception, please try again.")
}
mapBuffer
}
我也試過
def readFile(filename: String): Map[String, List[Int]] = {
var mapBuffer: Map[String, List[Int]] = Map()
try {
for (line <- Source.fromFile(filename).getLines()) {
val splitline = line.split(",").map(_.trim).toList.map(_.toInt)
mapBuffer = mapBuffer ++ Map(splitline.head -> splitline.tail)
}
} catch {
case ex: Exception => println("There was an exception, please try again.")
}
mapBuffer
}
但給我地圖[任意,列表[INT]]。任何幫助將不勝感激,謝謝。
另一個格拉斯哥HND課程的問題。你們不是有一個研究小組嗎? –