Go語言程式合併兩個切片


在 Go 語言中,切片就像陣列一樣,是一系列元素的序列。陣列是一系列固定元素的序列,而切片是動態陣列,這意味著它的值不是固定的,可以更改。切片比陣列更有效率和更快,此外它們是透過引用傳遞而不是透過值傳遞。

語法

func append(slice, element_1, element_2…, element_N) []T

append 函式用於向陣列切片新增值。它接受多個引數。第一個引數是要向其新增值的陣列,後跟要新增的值。然後,該函式返回包含所有值的最終陣列切片。

func make ([] type, size, capacity)

Go 語言中的 make 函式用於建立陣列/對映,它接受要建立的變數型別、其大小和容量作為引數

func copy(dst, str[] type) int

Go 語言中的 copy 函式用於將一個源陣列的值複製到目標陣列,並將複製的元素數量作為結果返回。它以兩個陣列作為引數。

演算法

  • 步驟 1 − 建立一個包 main 並宣告 fmt(格式化包)包在程式中,其中 main 生成可執行程式碼,fmt 有助於格式化輸入和輸出。

  • 步驟 2 − 建立一個函式 main,並在該函式中建立一個切片 slice1 並用一些值填充它。

  • 步驟 3 − 建立另一個切片 slice2 並用一些值填充它,以便它可以與 slice1 合併。

  • 步驟 4 − 使用 append 函式將 slice1 和 slice2 合併到 slice3 中,並在控制檯上列印此新切片。

  • 步驟 5 − 這裡 .. 表示 slice2 的元素作為單個引數而不是單個切片傳遞。

  • 步驟 6 − 使用 fmt.Println() 函式在控制檯上執行列印語句,其中 ln 表示換行符。

示例 1

在本例中,我們將學習如何使用 append 函式(一種內建函式)合併兩個切片。將建立兩個切片,並將它們附加到一個切片中,該切片將在控制檯上打印出來。讓我們透過演算法和程式碼來理解它。

package main
import "fmt"
func main() {
   slice1 := []int{10, 20, 30} //create slice1
   fmt.Println("The elements of slice1 are:", slice1)
   slice2 := []int{40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", slice2)
   slice3 := append(slice1, slice2...) //append slice1 and slice2
   fmt.Println(slice3) //print the slice3 which contains both slice1 and slice2
}

輸出

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [40 50 60]
[10 20 30 40 50 60]

示例 2

在本例中,我們將學習如何使用 copy 函式合併兩個切片。在這裡,我們將建立兩個切片,並使用內建函式將它們的元素複製到另一個切片中。讓我們透過演算法和程式碼來理解它。

package main
import "fmt"
func main() {
   myslice1 := []int{10, 20, 30} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   myslice3 := make([]int, len(myslice1)+len(myslice2))
   copy(myslice3, myslice1)
   copy(myslice3[len(myslice1):], myslice2)
   fmt.Println("The elements of slice3 after the elements of above two slices merged are:")
   fmt.Println(myslice3) //print new slice with elements of both slice1 and slice2
}

輸出

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [40 50 60]
The elements of slice3 after the elements of above two slices merged are:
[10 20 30 40 50 60]

示例 3

在本例中,我們將使用 for 迴圈中的 append 函式合併兩個切片。在本例中將建立兩個切片,並在其中一箇中合併元素。讓我們深入瞭解演算法和程式碼,看看它是如何完成的。

package main
import "fmt"
func main() {
   myslice1 := []int{10, 20, 30} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   fmt.Println("The slice after its elements merged are:")
   for _, element := range myslice2 {
      myslice1 = append(myslice1, element) //using append function append slices
   }
   fmt.Println(myslice1) //print slice1
}

輸出

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [40 50 60]
The slice after its elements merged are:
[10 20 30 40 50 60]

結論

我們使用三個示例執行了合併兩個切片的程式。在第一個示例中,我們使用 append 函式合併兩個切片。在第二個示例中,我們使用 copy 函式合併切片,在第三個示例中,我們使用帶有 append 函式的 for 迴圈。這兩個函式都是內建函式。因此,程式成功執行。

更新於: 2023年2月13日

5K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告