Go 語言程式檢查兩個切片是否相等
在本文中,我們將瞭解如何透過相關的示例來檢查兩個切片是否相等。切片就像陣列一樣,是一系列元素的序列。陣列是固定元素序列,而切片是動態陣列,這意味著它的值不是固定的,可以更改。切片比陣列更高效、更快,此外,它們是透過引用而不是透過值傳遞的。讓我們透過示例學習這個概念。
方法 1:使用內建函式
在這種方法中,我們將使用 reflect 包中的 reflect.DeepEqual() 函式來檢查兩個切片是否相等。讓我們一起了解演算法和程式碼,看看它是如何執行的。
演算法
步驟 1 − 建立一個 package main 並宣告程式中的 fmt(格式包)包,其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。
步驟 2 − 建立一個函式 main,並在該函式中初始化一個 myslice1,並在其中新增一些值。
步驟 3 − 同樣建立另外兩個名為 myslice2 和 myslice3 的切片,並在這些切片中新增一些值。
步驟 4 − 使用 Go 語言中的 print 語句在控制檯上列印所有這些切片。
步驟 5 − 使用 reflect 包中的 reflect.DeepEqual(),在第一個案例中將此函式應用於 myslice1 和 myslice2,結果將以布林值的形式列印在控制檯上,表示這兩個切片是否相等。
步驟 6 − 使用相同的函式與 myslice1 和 myslice3,並以相同的方式在控制檯上列印輸出
示例
使用內建函式檢查兩個切片是否相等的 Go 語言程式
package main
import (
"fmt"
"reflect"
)
func main() {
myslice1 := []int{10, 20, 30} //create slice1
fmt.Println("The elements of slice1 are:", myslice1)
myslice2 := []int{10, 20, 30} //create slice2
fmt.Println("The elements of slice2 are:", myslice2)
myslice3 := []int{40, 50, 60} //create slice3
fmt.Println("The elements of slice3 are:", myslice3)
fmt.Println("Let's check whether the slices are equal or not")
fmt.Println("Are the slice1 and slice2 equal?")
fmt.Println(reflect.DeepEqual(myslice1, myslice2)) // true
fmt.Println("Are the slice1 and slice3 equal?")
fmt.Println(reflect.DeepEqual(myslice1, myslice3)) // false
}
輸出
The elements of slice1 are: [10 20 30] The elements of slice2 are: [10 20 30] The elements of slice3 are: [40 50 60] Let's check whether the slices are equal or not Are the slice1 and slice2 equal? true Are the slice1 and slice3 equal? false
方法 2:遍歷切片元素
在這種方法中,我們將透過遍歷切片的元素來檢視兩個切片是否相等。我們將比較切片的元素並找到結果。讓我們一起了解演算法和程式碼,看看它是如何執行的。
演算法
步驟 1 − 建立一個 package main 並宣告程式中的 fmt(格式包)包,其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。
步驟 2 − 建立一個函式 main,並在該函式中初始化一個 myslice1,並在其中新增一些值。
步驟 3 − 同樣建立另一個名為 myslice2 的切片,並在該切片中新增一些值。
步驟 4 − 使用 Go 語言中的 print 語句在控制檯上列印所有這些切片。
步驟 5 − 建立一個名為 slice_equality 的函式,其引數為 myslice1 和 myslice2,函式返回的值型別為布林型。
步驟 6 − 檢查條件:如果 myslice1 和 myslice2 的長度不相等,則向函式返回 false。
步驟 7 − 執行一個迴圈,直到 myslice1 的長度,並比較兩個切片的元素。
步驟 8 − 如果兩個切片的元素不相等,則返回 false,但如果它們相等,則向函式返回 true。
步驟 9 − 輸出將使用 fmt.Println() 函式列印,其中 ln 表示換行。
示例
透過遍歷切片元素檢查兩個切片是否相等的 Go 語言程式。
package main
import (
"fmt"
)
func main() {
myslice1 := []int{10, 20, 30} //create slice1
fmt.Println("The elements of slice1 are:", myslice1)
myslice2 := []int{10, 20, 30} //create slice2
fmt.Println("The elements of slice2 are:", myslice2)
fmt.Println("Let's check whether the slices are equal or not")
fmt.Println("Are the slice1 and slice2 equal?")
fmt.Println(slice_equality(myslice1, myslice2)) // true
}
func slice_equality(myslice1, myslice2 []int) bool {
if len(myslice1) != len(myslice2) { //if condition is not satisfied print false
return false
}
for i, element := range myslice1 { // use for loop to check equality
if element != myslice2[i] {
return false
}
}
return true
}
輸出
The elements of slice1 are: [10 20 30] The elements of slice2 are: [10 20 30] Let's check whether the slices are equal or not Are the slice1 and slice2 equal? true
結論
在上面的程式中,我們使用了兩個示例來檢查兩個切片是否相等。在第一種方法中,我們使用了 reflect 包的函式,在第二種方法中,我們使用了 for 迴圈來比較切片並檢查它們是否相等。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP