scala 数组合并_Scala程序合并两个数组或数组缓冲区
scala 數組合并
Scala | 合并兩個數組 (Scala | Merging two arrays)
Arrays are important data structures in programming and there may arise times when we have two different arrays and we need to merge them into one for processing. This is the case when you need array concatenation which is the process to merge to arrays by appending the elements of the second array after the elements of the first array.
數組是編程中重要的數據結構,當我們有兩個不同的數組并且需要將它們合并為一個數組進行處理時,可能會出現這種情況。 在需要數組級聯的情況下就是這種情況,這是通過將第二個數組的元素追加到第一個數組的元素之后而合并到數組的過程。
Arrays in Scala are generally immutable, so we need to create a new array that will store the concatenated array in Scala. Or another method could be creating mutable arrays that will save memory.?
Scala中的數組通常是不可變的,因此我們需要創建一個新數組,該數組將在Scala中存儲級聯數組。 或者另一種方法是創建可節省內存的可變數組。
For merging two arrays Scala provides you multiple methods and we will discuss them one by one in this article.
為了合并兩個數組,Scala為您提供了多種方法,我們將在本文中逐一討論。
1)使用concat()方法 (1) Using the concat() method)
In Scala, there is a method named concat() that is used to concatenate two arrays.
在Scala中,有一個名為concat()的方法用于連接兩個數組。
Syntax:
句法:
concat(array_1, array_2)This method returns an array which is a concatenated array.
此方法返回一個數組,該數組是一個級聯數組。
Program to concatenate two arrays using concat() method
程序使用concat()方法連接兩個數組
object myObject {def main(args: Array[String]) {val array1 = Array(56, 12, 67) print("Array 1: ") for(i <- 0 to array1.length-1)print(array1(i)+" ")val array2 = Array(83, 45, 90) print("\nArray 2: ") for(i <- 0 to array2.length-1)print(array2(i)+" ")val array3 = Array.concat(array1, array2) print("\nMerged array: ") for(i <- 0 to array3.length-1)print(array3(i)+" ") } }Output
輸出量
Array 1: 56 12 67 Array 2: 83 45 90 Merged array: 56 12 67 83 45 902)使用++方法 (2) Using the ++ method)
To merge two arrays into one ++ method can also be used.
將兩個數組合并為一個++方法也可以使用。
Syntax:
句法:
array1 ++ array2This will return the merge array.
這將返回合并數組。
Program to concatenate two arrays using ++ method
程序使用++方法連接兩個數組
object myObject {def main(args: Array[String]) {val array1 = Array("Include", "Help") print("Array 1: ") for(i <- 0 to array1.length-1)print(array1(i)+" ")val array2 = Array("Learn", "Programming") print("\nArray 2: ") for(i <- 0 to array2.length-1)print(array2(i)+" ")val array3 = array1 ++ array2print("\nMerged array: ") for(i <- 0 to array3.length-1)print(array3(i)+" ") } }Output
輸出量
Array 1: Include Help Array 2: Learn Programming Merged array: Include Help Learn Programming合并兩個可變數組(arrayBuffers) (Merging two mutable arrays (arrayBuffers))
We can assign the concatenated array to any of the existing arrays when they are mutable. Both methods, concat() and ++ apply to ArrayBuffer also. A method assignment method can also be applied to the ArrayBuffer which is +=.
當它們可變時,我們可以將級聯數組分配給任何現有數組。 concat()和++這兩種方法也適用于ArrayBuffer 。 方法分配方法也可以應用于+ =的ArrayBuffer 。
Program to concatenate two ArrayBuffer into one
程序將兩個ArrayBuffer連接為一個
import scala.collection.mutable.ArrayBufferobject myObject {def main(args: Array[String]) {val array1 = ArrayBuffer(12, 43, 54) print("Array 1: ") for(i <- 0 to array1.length-1)print(array1(i)+" ")val array2 = ArrayBuffer(465, 787, 99) print("\nArray 2: ") for(i <- 0 to array2.length-1)print(array2(i)+" ")array1 ++= array2print("\nMerged array: ") for(i <- 0 to array1.length-1)print(array1(i)+" ") } }Output
輸出量
Array 1: 12 43 54 Array 2: 465 787 99 Merged array: 12 43 54 465 787 99翻譯自: https://www.includehelp.com/scala/program-to-merge-two-arrays-or-array-buffer.aspx
scala 數組合并
總結
以上是生活随笔為你收集整理的scala 数组合并_Scala程序合并两个数组或数组缓冲区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个类可以有一个接口,接口可以有一个Ja
- 下一篇: observable_Java Obse