Printing Array in Scala

Printing Array in Scala

Suraj P Feb-27, 2022 Scala Scala Array
  1. Use the for Loop to Print Array Elements
  2. Use foreach Loop to Print Array Elements
  3. Use mkstring() Method to Print Elements of an Array
  4. Converting Array to Another Scala Collection
  5. Use ScalaRunTime Object to Print Elements of an Array

An array is a data structure/collection that stores similar data. In Scala, an array is equivalent to Java native Arrays.

When we try to print the contents of an array in Scala, we get the output of the Array.toString method, which in most cases is the object hashcode.

This article will show you how to print an array in various ways using Scala.

Use the for Loop to Print Array Elements

The most commonly used solution is the for loop to print arrays in Scala. We iterate through array elements and print them one by one.

Here array indices are used to access the elements.

Syntax:

for(i <- 0 to array_name.length-1)
        print(array_name[i])

Example Code:

object MyClass {

    def main(args: Array[String]):Unit= {

        var marks = Array(97, 100, 69, 78, 40, 50, 99)
        println("Array elements are : ")
        for(i <- 0 to marks.length-1 )
            print(marks(i) + " ")
    }
}

Output:

Array elements are :
97 100 69 78 40 50 99

Use foreach Loop to Print Array Elements

Inside foreach, we can also write print or printf if we want the output in a single line.

Syntax:

array_name.foreach(println)

Example Code:

object MyClass {

    def main(args: Array[String]):Unit= {

        var marks = Array(97, 100, 69, 78, 40, 50, 99)
        println("Array elements are : ")
        marks.foreach(println)
    }
}

Output:

Array elements are :
97
100
69
78
40
50
99

The next example shows a looping concept that can easily be extended to multidimensional Arrays.

Example Code:

object MyClass {

    def main(args: Array[String]):Unit= {

        var arr = Array(Array(11, 22, 33, 44, 55),
                       Array(111,222, 333, 444, 555))

        for(i<- 0 to 1)
        {
            for(j<- 0 to 4)
            {
                print(" "+arr(i)(j))
            }
            println() //for empty line
        }
    }
}

Output:

 11 22 33 44 55
 111 222 333 444 555

Use mkstring() Method to Print Elements of an Array

We will first convert the array into a string then print that string. Using the mkstring() method to convert an Array element by element to string representations.

Example Code:

object MyClass {

    def main(args: Array[String]):Unit= {

        var marks = Array(97, 100, 69, 78, 40, 50, 99)
        print(marks.mkString(", "))
    }
}

Output:

97, 100, 69, 78, 40, 50, 99

Inside mkString() we passed , to print comma delimited output.

Converting Array to Another Scala Collection

Though it’s not a commonly used solution, we can convert the array to another Scala collection like List and then print it directly. We will use the toList method to convert an array into a List.

Example Code:

object MyClass {

    def main(args: Array[String]):Unit= {

        var marks = Array(97, 100, 69, 78, 40, 50, 99)
        val l = marks.toList
        print(l)
    }
}

Output:

List(97, 100, 69, 78, 40, 50, 99)

Use ScalaRunTime Object to Print Elements of an Array

Another solution is to use the ScalaRunTime object.

Example Code:

object MyClass {

    def main(args: Array[String]):Unit= {

        var marks = Array(97, 100, 69, 78, 40, 50, 99)
        print(runtime.ScalaRunTime.replStringOf(marks, marks.length))
    }
}

Output:

Array(97, 100, 69, 78, 40, 50, 99)
Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - Scala Array

  • Sort an Array in Scala
  • Find Array Size of an Element in Scala
  • Fill an Array in Scala
  • Appending Elements to an Array in Scala