3

I want to create an array that contains the same value repeated for a very large number of times, say 1,000,000.

I was thinking to use something like Array.fill(1000000)(0). However, after reading the documentation for Scala 2.11.8, I found that there is no such members of Array in this version.

Is there any other ways that can create the array without using loop? Thanks in advance for your help.

1
  • Have your tried fill yourself? The fill method already existed in scala 2.11.8. Commented Sep 3, 2017 at 5:05

3 Answers 3

5

This will do the trick:

Array.fill[Int](1000000)(0)

Read more here: https://alvinalexander.com/scala/scala-list-class-examples

Sign up to request clarification or add additional context in comments.

Comments

1

You can use range to iterate through required length (1000000 times in your case) and then return a default value which is 0 in each iteration as below.

val arr:Array[Int] = (1 to 1000000 map(_ => 0)).toArray

Comments

1

Stream.continually(0).take(1000000).toArray would do that .. but why in the world would you want something like this???

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.