I want to convert a range of Int into a a List or an Array. I have this code working in Scala 2.8:
var years: List[Int] = List()
val firstYear = 1990
val lastYear = 2011
firstYear.until(lastYear).foreach(
e => years = years.:+(e)
)
I would like to know if there is another syntax possible, to avoid using foreach, I would like to have no loop in this part of code.
Thanks a lot!
Loic
You can use toList
method:
scala> 1990 until 2011 toList
res2: List[Int] = List(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010)
toArray
method converts Range
to array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With