In Scala, we can easily create an array of integers using the new
keyword. We need to call the new
keyword to create an array.
var ArrayVariable = new Array[Int](numberOfElements)
ArrayVariable
: This is the array variable that will be initialised with some elements.Int
: This parameter specifies that the array must be of type integer.numberOfElements
: This is the number of elements in an array. We use the new
keyword to convert the array variable to an array instance. We use the int
to specify type integer. This means that any of its elements must be an integer.
object Main extends App { // create array of integers with "new" keyword var OurArray = new Array[Int](3); OurArray(0) = 10; OurArray(1) = 3; OurArray(2) = 1; // print out array values separated by commas println(OurArray.mkString(",")); }
ourArray
by using the keyword new
. mkString()
method to print the elements of the array in form of a string separated by commas.RELATED TAGS
CONTRIBUTOR
View all Courses