日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scala中的二维数组_Scala中的多维数组

發布時間:2025/3/11 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scala中的二维数组_Scala中的多维数组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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編程中,有兩種用于定義多維數組的方法 ,它們是:

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

    Array.ofDim :Scala具有內置的ofDim()方法,該方法創建多維數組 。

  • 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) = 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 = 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中的多维数组的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。