Home » Scala language

Arrays in Scala

An array is a data Structure, which stores a collection of data of similar data type. In this Scala tutorial, we will learn about arrays and their implementation with examples and codes.
Submitted by Shivang Yadav, on July 01, 2019

Arrays in Scala

An array is a linear data structure with a fixed number of elements. It is a collection that stores a fixed number Arrays in Scalf elements of the same datatype. In Scala, an array is 0 indexed, i.e. the first element has an index of zero. The last element of the array has an index of the last element minus one.

The syntax of scala array creation is just the same as in Java but is a lot more powerful in term of features and methods backing it. It can also support sequence functions in Scala. In Scala, for defining array there is liberty on the data type. i.e. you can skip assigning of the datatype of the array. Also, it supports all types of elements.

An array can extend up to as many dimensions as you want but only 1-D, 2-D, and 3-D arrays are commonly used. Here, we will discuss only a one-dimensional array.

ONE DIMENSIONAL ARRAY

A one-dimensional array is one which has only one row that stores data in it. It uses contagious memory allocation for elements at index 0 to total minus one.

Syntax:

    // its only single dimension...
    var arrayname = new Array[datatype](size)  

Methods to create an array in Scala

You can optionally specify the data type of the array in Scala.

    // It this we have specified that the array will contain string explicitly. 
    val name: String = new Array("Ram", "Akash", "Palak", "Geeta", "Sudhir");  

    //In this creation method the  will itself make 
    //the array of double datatype.
    val percentage = new Array(46.4 , 87.4 , 76.2 , 56.9 , 89.87)   

Example:

object MyClass {
      def add(x:Int, y:Int) = x + y;

      def main(args: Array[String]) {
          var i=0
          var name =  Array("Ram", "Akash", "Palak", "Geeta", "Sudhir")
          var percentage =  Array (46.4 , 87.4 , 76.2 , 56.9 , 89.87)
          
          println("Printing student names");
          for(i <- 0 to name.length-1){
              println("Student " + name(i) + " has scored " + percentage(i) + "%")
          }
      }
   }

Output

Printing student names
Student Ram has scored 46.4%
Student Akash has scored 87.4%
Student Palak has scored 76.2%
Student Geeta has scored 56.9%
Student Sudhir has scored 89.87%





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.