开发者

Scala create List[Int]

How can I quickly create a List[开发者_如何学GoInt] that has 1 to 100 in it?

I tried List(0 to 100), but it returns List[Range.Inclusive]

Thanks


Try

(0 to 100).toList

The code you tried is creating a list with a single element - the range. You might also be able to do

List(0 to 100:_*)

Edit

The List(...) call takes a variable number of parameters (xs: A*). Unlike varargs in Java, even if you pass a Seq as a parameter (a Range is a Seq), it will still treat it as the first element in the varargs parameter. The :_* says "treat this parameter as the entire varargs Seq, not just the first element".

If you read : A* as "an (:) 'A' (A) repeated (*)", you can think of :_* as "as (:) 'something' (_) repeated (*)"


List.range(1,101)

The second argument is exclusive so this produces a list from 1 to 100.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜