Swift程式搜尋陣列中的元素


在本文中,我們將學習如何編寫一個Swift程式來搜尋陣列中的元素。這裡我們使用以下方法從給定陣列中搜索元素:搜尋

  • 使用 == 運算子

  • 使用 contains() 方法

  • 使用 in 函式

方法 1:使用 == 運算子

要從給定陣列中搜索元素,我們遍歷給定陣列的每個元素,並使用 == 運算子檢查該元素是否等於指定元素。如果為真,則指定元素存在於陣列中。否則不。

演算法

步驟 1 − 建立一個函式。

步驟 2 − 在此函式中,使用 for 迴圈遍歷陣列的每個元素。

步驟 3 − 使用 == 運算子檢查當前元素是否等於指定元素。

步驟 4 − 如果在陣列中找到指定元素,則此函式返回 true。否則返回 false。

步驟 5 − 建立一個整數型別的陣列。

步驟 6 − 呼叫函式並將我們想要搜尋的陣列和元素作為引數傳遞給它。

步驟 7 − 列印輸出。

示例

以下Swift程式用於搜尋陣列中的元素

import Foundation
import Glibc

// Function to search specified element in the array
func searchElement(array: [Int], element: Int) -> Bool {
   for ele in array {
      if ele == element {
         return true
      }
   }
   return false
}

// Test case
let arr = [1, 2, 3, 4, 5]
print("Is 4 present in the given array?", searchElement(array: arr, element: 4))
print("Is 10 present in the given array?", searchElement(array: arr, element: 10))

輸出

Is 4 present in the given array? true
Is 10 present in the given array? false

在上面的程式碼中,我們有一個整數型別的陣列。現在我們建立一個函式來檢查給定元素是否存在於陣列中。因此,我們遍歷陣列的每個元素,並檢查當前元素是否等於指定元素。如果在給定陣列中找到該元素,則迴圈結束,此函式返回 true。否則,檢查過程將繼續直到陣列結束,此函式返回 false。

方法 2:使用 contains() 函式

要從給定陣列中搜索元素,我們還可以使用 contains() 函式。此函式用於檢查指定元素是否存在於給定陣列中。如果它返回 true,則表示指定元素存在於陣列中。或者,如果它返回 false,則表示指定元素不存在於陣列中

演算法

步驟 1 − 建立一個函式。

步驟 2 − 在此函式中,使用 contains() 函式檢查當前元素是否等於指定元素。

步驟 3 − 建立一個整數型別的陣列。

步驟 4 − 呼叫函式並將我們想要搜尋的陣列和元素作為引數傳遞給它。

步驟 5 − 列印輸出。

示例

以下Swift程式用於搜尋陣列中的元素

import Foundation
import Glibc

// Function to search specified element in the array
func searchElement(array: [Int], ele: Int) -> Bool {
   return array.contains(ele)
}

// Test case
let arr = [34, 5, 67, 32, 4, 56, 6, 54, 3]
print("Is 32 present in the given array?", searchElement(array: arr, ele: 32))
print("Is 11 present in the given array?", searchElement(array: arr, ele: 11))

輸出

Is 32 present in the given array? true
Is 11 present in the given array? false

在上面的程式碼中,我們有一個整數型別的陣列。現在我們建立一個函式來檢查給定元素是否存在於陣列中。因此,我們使用 contains() 函式來檢查指定元素是否存在於給定陣列中。如果在陣列中找到該元素,則 contains() 函式將返回 true。否則,它將返回 false。

方法 3:使用函式

要從給定陣列中搜索元素,我們還可以使用使用者定義的函式,在其中我們檢查給定元素是否存在於陣列中。

示例

以下Swift程式用於搜尋陣列中的元素

import Foundation
import Glibc

// Function to search specified element in the array
func searchElement(array: [Int], ele: Int) -> Bool {
   var foundele = false
   var i = 0
   while !foundele && i < array.count {
      if array[i] == ele {
         foundele = true
      }
      else {
         i += 1
      }
   }
   return foundele
}

// Test case
let array = [34, 2, 56, 3, 56, 7, 88]
print("Is element = 4 is present in the array?", searchElement(array: array, ele: 4))
print("Is element = 88 is present in the array?", searchElement(array: array, ele: 88)) 

輸出

Is element = 4 is present in the array? false
Is element = 88 is present in the array? true

在上面的程式碼中,searchElement 函式使用 while 迴圈遍歷陣列的元素。它使用一個 found 標誌來跟蹤是否已找到指定元素。如果找到指定元素,則將標誌設定為 true 並終止迴圈。如果未找到指定元素,則迴圈將繼續直到到達陣列的末尾。然後,該函式返回 found 標誌的值。

結論

因此,這就是我們如何使用討論的方法在陣列中搜索元素。所有方法都適用於任何資料型別(稍作修改)。

更新於:2023年1月9日

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告