博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala中的二维数组_Scala中的多维数组
阅读量:2528 次
发布时间:2019-05-11

本文共 4129 字,大约阅读时间需要 13 分钟。

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 = 2Element 01 = 7Element 10 = 12Element 11 = 43Element 20 = 436Element 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 = 2Element 01 = 5Element 02 = 6Element 10 = 12Element 11 = 54Element 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行的列数也可以不同。

翻译自:

scala中的二维数组

转载地址:http://ekxzd.baihongyu.com/

你可能感兴趣的文章
学习笔记-模块之xml文件处理
查看>>
简单的栈和队列
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
点语法
查看>>
IO之Socket网络编程
查看>>
PCRE demo【转】
查看>>
矩阵的SVD分解
查看>>
gitlab的配置
查看>>
linux访问ftp服务器命令
查看>>
ActiveMQ学习笔记之异常
查看>>
LuoguP4012 深海机器人问题(费用流)
查看>>
自动机
查看>>
java知识点
查看>>
WPF设计の画刷(Brush)
查看>>
HTML学习笔记
查看>>
selinux详解及配置文件
查看>>
性能优化之数据库优化
查看>>