scala元组 数组_Scala中的数组

scala元组 数组

Scala中的数组 (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.

数组是具有固定数量元素的线性数据结构 。 它是一个集合,该集合在相同数据类型的Scalf元素中存储固定数量的Array。 在Scala中, 数组的索引为0,即第一个元素的索引为零。 数组的最后一个元素的索引为最后一个元素减一。

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.

Scala数组创建语法与 Java中的语法相同,但是在支持它的功能和方法方面要强大得多。 它还可以在Scala中支持序列功能。 在Scala中,用于定义数组的数据类型具有自由性。 即,您可以跳过数组数据类型的分配。 此外,它支持所有类型的元素。

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.

数组可以扩展到任意数量的维,但是通常仅使用1-D2-D3-D数组 。 在这里,我们将只讨论一维数组

一维数组 (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.

一维数组是只有一行存储数据的数组 。 它使用传染性内存分配将索引0的元素总计减为1。

Syntax:

句法:

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

Methods to create an array in Scala

在Scala中创建数组的方法

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

您可以选择在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%


翻译自: https://www.includehelp.com/scala/arrays-in-scala.aspx

scala元组 数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值