scala中的二维数组_Scala中的多维数组
scala中的二維數(shù)組
多維數(shù)組 (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.
一個(gè)以多維矩陣形式存儲(chǔ)數(shù)據(jù)的數(shù)組 。 多維數(shù)組通常用于在編程中制作矩陣和表。
In Scala programming, there are two methods used to define a multidimensional array, They are :
在Scala編程中,有兩種用于定義多維數(shù)組的方法 ,它們是:
Array.ofDim: Scala has inbuilt ofDim() method that creates multi-dimensional array.
Array.ofDim :Scala具有內(nèi)置的ofDim()方法,該方法創(chuàng)建多維數(shù)組 。
Array of Array: Array of Array is used to create ragged array in Scala programming language.
數(shù)組數(shù)組 :數(shù)組數(shù)組用于使用Scala編程語(yǔ)言創(chuàng)建參差不齊的數(shù)組。
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編程語(yǔ)言已經(jīng)定義了一種內(nèi)置方法ofDim()來(lái)創(chuàng)建多維數(shù)組 。 使用這種方法,您可以制作一個(gè)高達(dá)5維的陣列。 但是,使用此方法創(chuàng)建數(shù)組時(shí),應(yīng)創(chuàng)建時(shí)精確的行數(shù)和列數(shù)。 首先需要使用所需的行數(shù)和列數(shù)創(chuàng)建數(shù)組,然后用元素值填充數(shù)組。
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.
這兩種語(yǔ)法在Scala中均有效,可以創(chuàng)建多維數(shù)組 。
Example:
例:
object myObject { def main(args: Array[String]) { val multiArr= Array.ofDim[Int](3,2) multiArr(0)(0) = 2 multiArr(0)(1) = 7multiArr(1)(0) = 12multiArr(1)(1) = 43multiArr(2)(0) = 436multiArr(2)(1) = 672for(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 = 672Code explanation:
代碼說(shuō)明:
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()方法創(chuàng)建數(shù)組的過(guò)程 。 該代碼創(chuàng)建一個(gè)具有3行2列的二維數(shù)組 。 我們已經(jīng)通過(guò)了3,2作為參數(shù)。 接下來(lái),在代碼中,我們已經(jīng)初始化了數(shù)組中每個(gè)元素的值。 最后,我們使用了帶有2個(gè)變量的for循環(huán)來(lái)打印數(shù)組的值。 打印語(yǔ)句類似于此元素ij = value 。
2)數(shù)組數(shù)組 (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.
創(chuàng)建多維數(shù)組的另一種方法。 數(shù)組的數(shù)組創(chuàng)建一個(gè)堅(jiān)固的數(shù)組 。 堅(jiān)固陣列是每個(gè)包含不同大小的陣列的陣列。 因此,創(chuàng)建數(shù)組數(shù)組是一種優(yōu)雅的方法。 在此數(shù)組中,我們無(wú)法將初始化和值饋送分開(kāi)。
Syntax:
句法:
var arrayName = Array(Array(elements), Array(elements))Syntax explanation:
語(yǔ)法說(shuō)明:
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.
請(qǐng)記住,可以對(duì)數(shù)組進(jìn)行加固 ,以完成這種類型的初始化。 因此,這就是為什么我們定義了一個(gè)以數(shù)組為元素的數(shù)組的原因。 每個(gè)數(shù)組可以有自己的大小。 但是數(shù)據(jù)類型應(yīng)該相同。
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 = 232Code explanation:
代碼說(shuō)明:
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的數(shù)組初始化多維數(shù)組 。 借助此方法,我們將陣列做成了2行3列,而且第1行和第2行的列數(shù)也可以不同。
翻譯自: https://www.includehelp.com/scala/multi-dimensional-array-in-scala.aspx
scala中的二維數(shù)組
總結(jié)
以上是生活随笔為你收集整理的scala中的二维数组_Scala中的多维数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c++ stl队列初始化_创建一个向量,
- 下一篇: c++ scanf读取_使用scanf(