Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 10:18:19 Viewed : 565
Scala Single dimensionalArray
In Scala, you can create a single-dimensional array using the Array
class. Here is how to create and work with single-dimensional arrays, along with examples and their outputs:
Creating a Single-Dimensional Array:
You can create a single-dimensional array in Scala by providing the data type and initializing the array with values. Here is an example using an array of integers:
scalaval numbers: Array[Int] = Array(1, 2, 3, 4, 5)
Alternatively, you can also create an empty array and then populate it:
scalaval numbers: Array[Int] = new Array[Int](5) numbers(0) = 1 numbers(1) = 2 numbers(2) = 3 numbers(3) = 4 numbers(4) = 5
Accessing Elements:
You can access elements of a single-dimensional array using their indices. Array indices in Scala start at 0.
scalaval thirdElement = numbers(2) // Access the third element (index 2)
Modifying Elements:
You can modify elements of an array by assigning new values to them.
scalanumbers(1) = 10 // Change the second element (index 1) to 10
Iterating Over Elements:
You can use loops or higher-order functions like foreach
to iterate over the elements of an array. Here is an example using a for
loop:
scalafor (i <- 0 until numbers.length) { println(numbers(i)) }
Or you can use the foreach
method:
scalanumbers.foreach(println)
Example with Output:
Lets put it all together with an example that creates an array of integers, modifies some elements, and then prints the elements:
scalaobject ArrayExample { def main(args: Array[String]): Unit = { val numbers: Array[Int] = Array(1, 2, 3, 4, 5) // Modify an element numbers(1) = 10 // Print all elements println("Array elements:") numbers.foreach(println) } }
Output:
javascriptArray elements:
1
10
3
4
5
In this example, we create an array of integers, change the second element to 10, and then print all the elements of the array.
final classArray[T] extends java.io.Serializable with java.lang.Cloneable
Arrays are mutable, indexed collections of values. Array[T] is Scala(s) representation for Java(s) T[].
1.
val numbers = Array(1, 2, 3, 4)
2.
val first = numbers(0) // read the first element
numbers(3) = 100 // replace the 4th array element with 100
3. val biggerNumbers = numbers.map(_ * 2) // multiply all numbers by two
Arrays make use of two common pieces of Scala syntactic
sugar, shown on lines 2 and 3 of the above example code. Line 2 is translated
into a call to apply(Int), while
line 3 is translated into a call to update(Int,
T).
The conversion to ArrayOps takes priority over the
conversion to ArraySeq. For instance, consider the following code:
val arr = Array(1,
2, 3)
val arrReversed = arr.reverse
val seqReversed : collection.Seq[Int] = arr.reverse
var arrayName : Array[arrayType] = new Array[arrayType](arraySize); or
var arrayName = new Array[arrayType](arraySize) or
var arrayName : Array[arrayType] = new Array(arraySize); or
var arrayName = Array(element1, element2 ... elementN)
Following example illustrates about Scala single
dimensional Array
Save the file as − SingleDimeArray.scala.
SingleDimeArray.scala
package runnerdev
class ArrayDeclare {
var arr = Array(1, "cat", "dog", 4, 5)
// Creating single
dimensional array
def show() {
for (a <- arr) // Traversing array elements
println(a)
println("Third Element is " + arr(2)) //
Accessing elements by using index
}
}
object SingleDimeArray {
def main(args: Array[String]) {
var a = new
ArrayDeclare()
a.show()
}
}
compile
and run the above example as follows
scala> scalac SingleDimeArray.scala
scala> scala SingleDimeArray
Output:
1
cat
dog
4
5
Third Element is dog
Example 2:
Following example
illustrates about Scala Sinlge dimensional Array
Save the file as − SingleDimeArray2.scala
SingleDimeArray2.scala
package runnerdev
class ArrayDeclare2 {
var arr = new Array[Int](5) // Creating single
dimensional array
arr(0) = 10
arr(1) = 11;
arr(2) = 12;
arr(3) = 13;
def show() {
for (x <- arr) { // Traversing array elements
println(x)
}
println("Third
Element before assignment = " + arr(3)) // Accessing elements by
using index
arr(3) = 11 // Assigning new element
at 2 index
println("Third
Element after assignment = " + arr(3))
}
}
object SingleDimeArray2 {
def main(args: Array[String]) {
var arrayObj = new ArrayDeclare2()
arrayObj.show()
}
}
compile
and run the above example as follows
scala> scalac ArrayDeclare2.scala
scala> scala ArrayDeclare2
Output:
10
11
12
13
0
Third Element before assignment
= 13
Third Element after assignment
= 11