How to read numbers of n lines into a Scala list?

This is for the online judge. I am trying to learn some Scala in the process.

The input format looks like

4
543534
6756
4564
363773

      

So the first line is n, and then the next n lines contain the items to go to the list.

Right now I'm reading n using readInt()

, but I don't know how to say, "Now read the next n lines and put everything in a list."

+3


source to share


2 answers


Try the following:



val n = io.StdIn.readInt
val list = ( 0 to n ).map( (x) -> io.StdIn.readLine ).toList
//...

      

+1


source


This can be done on one line if you trust this input and ignore this first range value.



val list = io.StdIn.getLines.drop(1).map(_.toInt).toList

      

0


source







All Articles