tags:

views:

170

answers:

2

Hi,

How can I quickly create a List[Int] that has 1 to 100 in it?

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

Thanks

+13  A: 

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 (*)"

Ben Lings
Thanks, what does :_* do in List(0 to 100:_*) ?
portoalet
+7  A: 
List.range(0,101)
Eastsun