scala中的二维数组

多维数组 (Multi-dimensional arrays)

An Array that stores data in the form multidimensional matrix. Multidimensional arrays are generally used for making matrices and tables in programming.

一个以多维矩阵形式存储数据的数组多维数组通常用于在编程中制作矩阵和表。

In Scala programming, there are two methods used to define a multidimensional array, They are :

在Scala编程中,有两种用于定义多维数组的方法 ,它们是:

  1. Array.ofDim: Scala has inbuilt ofDim() method that creates multi-dimensional array.

    Array.ofDim :Scala具有内置的ofDim()方法,该方法创建多维数组

  2. Array of Array: Array of Array is used to create ragged array in Scala programming language.

    数组数组 :数组数组用于使用Scala编程语言创建参差不齐的数组。

1)Array.ofDim方法 (1) Array.ofDim Method)

Scala programming language has defined an inbuilt method ofDim() to create a multidimensional array. Using this method you can make an array upto 5-D. But to create an array with this method the exact number of rows and columns at the time of creation. The array first needs to be created first with the number of rows and columns you need and then your array is filled with values of elements.

Scala编程语言已经定义了一种内置方法ofDim()来创建多维数组 。 使用这种方法,您可以制作一个高达5维的阵列。 但是,使用此方法创建数组时,应创建时精确的行数和列数。 首先需要使用所需的行数和列数创建数组,然后用元素值填充数组。

Syntax:

句法:

    var arrayName = Array.ofDim[data_type](row, column)
Or
var arrayName = ofDim[data_type](row, column)

Both syntaxes are valid in Scala to create a multidimensional array.

这两种语法在Scala中均有效,可以创建多维数组

Example:

例:

object myObject
{
def main(args: Array[String])
{
val multiArr= Array.ofDim[Int](3,2)
multiArr(0)(0) = 2
multiArr(0)(1) = 7
multiArr(1)(0) = 12
multiArr(1)(1) = 43
multiArr(2)(0) = 436
multiArr(2)(1) = 672
for(i <- 0 to 2; j <- 0 to 1){println("Element "+ i  + j + " = " + multiArr(i)(j))
}
}
}

Output

输出量

Element 00 = 2
Element 01 = 7
Element 10 = 12
Element 11 = 43
Element 20 = 436
Element 21 = 672

Code explanation:

代码说明:

The above code is the show creation of an array using the ofDim() method. The code creates a two-dimensional array with 3 rows and 2 columns. We have passed 3,2 as an argument for this. Next, in the code, we have initialized the value of each element of the array. At last, we have used a for loop with 2 variables to print the values of the array. The print statement is like this Element ij = value.

上面的代码展示了使用ofDim()方法创建数组的过程 。 该代码创建一个具有3行2列的二维数组 。 我们已经通过了3,2作为参数。 接下来,在代码中,我们已经初始化了数组中每个元素的值。 最后,我们使用了带有2个变量的for循环来打印数组的值。 打印语句类似于此元素ij = value 。

2)数组数组 (2) Array of Array)

An alternate method to create a multidimensional array. The array of array creates a rugged array. A rugged array is an array that has each contained array of different sizes. Hence, it is an elegant method to create an array of arrays. In this array, we cannot separate initialization and value feeding.

创建多维数组的另一种方法。 数组的数组创建一个坚固的数组坚固阵列是每个包含不同大小的阵列的阵列。 因此,创建数组数组是一种优雅的方法。 在此数组中,我们无法将初始化和值馈送分开。

Syntax:

句法:

    var arrayName = Array(Array(elements), Array(elements))

Syntax explanation:

语法说明:

This type of initialization is done keeping in mind that the array can be rugged. So, this is why we have defined an array that has arrays as its elements. Each array can have its own size. But the datatype should be the same.

请记住,可以对数组进行加固 ,以完成这种类型的初始化。 因此,这就是为什么我们定义了一个以数组为元素的数组的原因。 每个数组可以有自己的大小。 但是数据类型应该相同。

Example:

例:

object myObject
{
def main(args: Array[String])
{
val multiArr= Array(Array(2,5,6),Array(12, 54,232))
for(i <- 0 to 1; j <- 0 to 2){println("Element "+ i  + j + " = " + multiArr(i)(j))
}
}
}

Output

输出量

Element 00 = 2
Element 01 = 5
Element 02 = 6
Element 10 = 12
Element 11 = 54
Element 12 = 232

Code explanation:

代码说明:

The above code initializes a multidimensional array using an array of array. We have made an array with 2 rows and 3 columns with the help of this method, also than the number of columns for row 1 and row 2 can be different.

上面的代码使用array的数组初始化多维数组 。 借助此方法,我们将阵列做成了2行3列,而且第1行和第2行的列数也可以不同。

翻译自: https://www.includehelp.com/scala/multi-dimensional-array-in-scala.aspx

scala中的二维数组

scala中的二维数组_Scala中的多维数组相关推荐

  1. scala中的高阶函数_Scala中的高阶函数(HOF)

    scala中的高阶函数 Higher Order Functions (HOF) in Scala are the very core of this functional programming l ...

  2. scala元组 数组_Scala中的数组

    scala元组 数组 Scala中的数组 (Arrays in Scala) An array is a linear data structure with a fixed number of el ...

  3. scala 类中的对象是类_Scala中的类和对象

    scala 类中的对象是类 Scala中的课程 (Classes in Scala) A class is a blueprint for objects. It contains the defin ...

  4. LeetCode 98. 验证二叉搜索树(中序遍历)

    文章目录 1. 题目信息 2. 解题 2.1 递归中序 2.2 非递归中序 1. 题目信息 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于 ...

  5. Verilog中的二维数组及其初始化

    Verilog中的二维数组 Verilog中提供了两维数组来帮助我们建立内存的行为模型.具体来说,就是可以将内存宣称为一个reg类型的数组,这个数组中的任何一个单元都可以通过一个下标去访问.这样的数组 ...

  6. foreach 二维java_教你如何用for和foreach循环遍历java中的二维数组

    一:先来说说for和foreach循环的区别 for和foreach的区别 foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach ...

  7. Java 中的二维数组

    所谓二维数组,可以简单的理解为是一种"特殊"的一维数组,它的每个数组空间中保存的是一个一维数组. 那么如何使用二维数组呢,步骤如下: 1. 声明数组并分配空间 或者 如: 2. 赋 ...

  8. Java黑皮书课后题第8章:*8.31(几何:交点)编写一个方法,返回两条直线的交点。四个点存放在4*2的二维数组points中。编写一个程序,提示用户输入4个点,并显示交点

    *8.31(几何:交点)编写一个方法,返回两条直线的交点.四个点存放在4*2的二维数组points中.编写一个程序,提示用户输入4个点,并显示交点 题目 题目描述 破题 代码 本题运行实例 题目 题目 ...

  9. python构建二维数组_Python中创建二维数组

    二维数组 二维数组本质上是以数组作为数组元素的数组,即"数组的数组",类型说明符 数组名[常量表达式][常量表达式].二维数组又称为矩阵,行列数相等的矩阵称为方阵.对称矩阵a[i] ...

最新文章

  1. Build SSCLI20 under VS2008 full Document (完全手册)
  2. 如何从grep -R中排除目录?
  3. shell学习之-sed用法解析_【Linux】shell学习之sed
  4. python进度条 pyqt_Python高级进阶#015 pyqt5进度条QProgressBar结合使用qbasictimer
  5. python设置路径_Python探索之修改Python搜索路径
  6. org.hibernate.annotationexception no identifier specified for entity
  7. SAP UI5 Opportunity popup
  8. 【啊哈!算法】之二、插入排序
  9. js php 中文乱码怎么解决_php中文乱码怎么解决
  10. php sjis,【通译】PHP中文字编码变换时使用SJIS-win而非SJIS,使用eucJP-win而非EUC-JP...
  11. 精品微信小程序后勤服务管理系统+后台管理系统|前后分离VUE
  12. 时间操作 - 时间格式转换
  13. Eclipse安装php插件phpeclipse(转)
  14. Nature:肠道内微生物合作方式的探究
  15. 免费的实时数据库,我们该选谁?----BerkeleyDB与SQLite评测对比
  16. 向量化回测系列1——单只股票的回测
  17. WPF MVVM架构 程序退出右下角托盘图标简单解决方案
  18. 03-SQLPlus的常用命令和使用
  19. MODBUS通信协议代码
  20. 计算机固态硬盘增大,老电脑想要焕发第二春,升级固态硬盘?还是加大内存容量?...

热门文章

  1. html中css二级联动,html二级联动学习笔记
  2. 郫都区计算机老师周俊老师,教师节,带你走进郫都教师背后的故事
  3. 轨道车辆垂向振动Matlab建模与仿真,基于matlab/simulink的车辆建模与故障分析
  4. vba 指定列后插入列_Excle中的VBA介绍分享
  5. php 做更新进度条,PHP exec()后更新Bootstrap进度条
  6. java ruby脚本_Java 程序中直接执行 Ruby 脚本 (JRuby)
  7. Hive-分区分桶操作
  8. java基础经典练习题
  9. SmoothNLP 中文NLP文本处理工具 Python 实战示范
  10. eclipse init 配置