📜  斯威夫特 - 数组

📅  最后修改于: 2022-05-13 01:54:27.175000             🧑  作者: Mango

斯威夫特 - 数组

数组是任何编程语言中最强大的数据结构之一。它用于存储相似数据类型的元素。换句话说,假设我们想要存储许多整数类型的值,那么在这种情况下我们可以使用整数数组,如果值是浮点类型,那么我们可以使用浮点数组。为了理解数组的重要性,让我们考虑一个例子。假设我们需要存储一个班级的学生的分数,那么我们创建一个浮点类型的变量来保存学生的分数,如果我们需要存储一个班级的两个学生的分数,那么我们可以创建两个变量可以保存这些学生的分数的浮动类型。现在如果我们说,我们需要存储班级所有学生的分数,并且班级的实力超过100或200甚至更多。现在我们无法创建这么多变量。为了处理这些问题,我们可以使用一个浮点数组并用值一个一个地初始化它。这提高了代码的可读性。本文重点介绍 Swift 中的数组。

大批

Swift 中的数组可以保存相同类型的值。对元素的唯一性没有任何限制。简而言之,元素可以在数组中重复任意次数。为了处理大量相似类型的数据项,数组非常有用。数组可以是任何数据类型,如字符串、 int 等。数组中的元素存储在指定位置,该位置称为数组索引。数组的索引从 0 开始,这意味着数组的第一个元素存储在位置 0,而不是 1,如下图所示:

我们可以使用以下语法创建一个数组:

句法:

这里,如果我们不指定数据类型,swift会根据赋值自动识别数据类型。

例子:

Swift
// Swift program to create an array
 
// Creating an array of type Int
var myArray: [Int] = [10, 20, 19]
var myArray1 = [11, 20, 29]
 
// Displaying array
print(myArray)
print(myArray)


Swift
// Swift program to create empty array
 
// Creating an empty array of type Int
var myArray: [Int] = []


Swift
// Swift program to demonstrate how to
// initialize an array with default values
 
// Initializing array
var myArray = Array(repeating: "GeeksforGeeks", count: 3)
 
// Print the array
print("myArray:", myArray)


Swift
// Swift program to access the elements of an array
 
// Creating an array of type Int
var myArray: [Int] = [10, 20, 19, 29, 45]
 
// Accessing the element of an array
// whose array index is 0
print("Element at index 0:", myArray[0])
 
// Accessing the element of an array
// whose array index is 3
print("Element at index 3:", myArray[3])


Swift
// Swift program to demonstrate how to
// modify values using subscript-syntax
 
var myArray: [String] = ["GeeksforGeeks", "GfG", "My", "DS"]
 
// Modifying values present at the index 0
// and at the index 1
myArray[0] = "Bhuwanesh"
myArray[1] = "Nainwal"
 
// Print the array after modification
print("myArray:", myArray)


Swift
// Swift program to illustrate how to modify
// values using subscript-syntax
 
// Initialize an array of strings
var myArray:[String] = ["GeekforGeeks", "Swift", "Java", "C++"]
 
// Modify values from 1 to 2 and assign values
myArray[1...2] = ["PHP", "HTML"]
 
// Modify values from 1 to 3 and assign values
myArray[1...3] = ["C++", "C#"]
 
// Print the array
print("myArray:", myArray)


Swift
// Swift program to demonstrate the working of
// insert() method used with arrays
 
// Initializing an array of strings
var myArray: [String] = ["Bhuwanesh", "Nainwal"]
 
// Insert at the index 2
myArray.insert("GeeksforGeeks", at:2)
 
// Print the array
print("myArray:", myArray)
 
// Insert at the index 3
myArray.insert("GFG", at:3)
 
// Print the array
print("myArray:", myArray)


Swift
// Swift program to demonstrate the working
// of append method used with an array
 
// Creating an empty array
var myArray: [String] = []
 
// Adding elements
// Using append method
myArray.append("GeeksforGeeks")
myArray.append("GFG")
myArray.append("Bhuwanesh")
myArray.append("Nainwal")
 
// Displaying result
print("myArray:", myArray)


Swift
// Swift program to demonstrate the working of
// (+) operator used to combine two arrays
 
// Initializing an array of integers
var myArray1: [Int] = [6, 1, 4]
 
// Initializing another array of integers
var myArray2: [Int] = [7, 3, 5, 2]
 
// Creating a new array by combining existing arrays
var newArray = myArray1 + myArray2
 
// Print the array
print("newArray:", newArray)


Swift
// Swift program to illustrate how to
// iterate over the array
 
// Initializing an array
var myArray = ["GeeksforGeeks", "Bhuwanesh", "Nainwal"]
 
// Using for-in loop to iterate over array elements
for element in myArray{
       
      // Print the current element
    print(element)
}


Swift
// Swift program to demonstrate the working
// of count method used with an array
 
// Array
var myArray: [Int] = [7, 3, 5, 2]
 
// Displaying the total elements of an array
// Using count property
print("Number of elements present in myArray:", myArray.count)


Swift
// Swift program to demonstrate the working
// of remove method used with arrays
 
// Initializing an array
var myArray: [String] = ["Bhuwanesh", "Nainwal",
                         "GeeksforGeeks", "Swift",
                         "Python", "Java"]
 
// Print the array
print("myArray:", myArray)
 
// Remove the element present at the index 1
myArray.remove(at: 1)
 
// Print the array
print("myArray:", myArray)
 
// Remove the element present at the index 3
myArray.remove(at: 3)
 
// Print the array
print("myArray:", myArray)


输出:

[10, 20, 19]
[10, 20, 19]

空数组

我们可以按照下面的初始化语法在 Swift 中创建一个空数组,

句法:

这里,dataType 是 myArray 存储的一种值。我们已经明确指定 myArray 的类型为“dataType”。现在,myArray 只能包含“dataType”的元素。

例子:

迅速

// Swift program to create empty array
 
// Creating an empty array of type Int
var myArray: [Int] = []

具有默认值的数组

Swift 提供了一个初始化器,我们可以使用它创建一个指定大小的数组,其中所有值都是默认值。

句法:

这里,myArray 是 String 类型,等于 [“GeeksforGeeks”,”GeeksforGeeks”, “GeeksforGeeks”]

例子:

迅速

// Swift program to demonstrate how to
// initialize an array with default values
 
// Initializing array
var myArray = Array(repeating: "GeeksforGeeks", count: 3)
 
// Print the array
print("myArray:", myArray)

输出:

myArray: ["GeeksforGeeks", "GeeksforGeeks", "GeeksforGeeks"]

访问和修改数组元素

众所周知,数组中的元素存储在指定的索引上,该索引称为数组索引。数组索引从 0 开始,因此数组的第一个元素出现在索引 0 而不是 1 处。因此在 Swift 中,我们可以借助下标语法使用数组索引访问元素。在下标语法中,我们在方括号中使用元素(我们想要访问的)的索引,就在数组名称之后。

句法:

例子:

迅速

// Swift program to access the elements of an array
 
// Creating an array of type Int
var myArray: [Int] = [10, 20, 19, 29, 45]
 
// Accessing the element of an array
// whose array index is 0
print("Element at index 0:", myArray[0])
 
// Accessing the element of an array
// whose array index is 3
print("Element at index 3:", myArray[3])

输出:

Element at index 0: 10
Element at index 3: 29

我们也可以在下标语法的帮助下修改数组的元素。

例子:

迅速

// Swift program to demonstrate how to
// modify values using subscript-syntax
 
var myArray: [String] = ["GeeksforGeeks", "GfG", "My", "DS"]
 
// Modifying values present at the index 0
// and at the index 1
myArray[0] = "Bhuwanesh"
myArray[1] = "Nainwal"
 
// Print the array after modification
print("myArray:", myArray)

输出:

myArray: ["Bhuwanesh", "Nainwal", "My", "DS"]

下标语法策略可用于更改数组值的范围,即使替换值集的长度与我们要替换的范围不同。

例子:

迅速

// Swift program to illustrate how to modify
// values using subscript-syntax
 
// Initialize an array of strings
var myArray:[String] = ["GeekforGeeks", "Swift", "Java", "C++"]
 
// Modify values from 1 to 2 and assign values
myArray[1...2] = ["PHP", "HTML"]
 
// Modify values from 1 to 3 and assign values
myArray[1...3] = ["C++", "C#"]
 
// Print the array
print("myArray:", myArray)

输出:

myArray: ["GeekforGeeks", "C++", "C#"]

向数组添加元素

Array 提供了多种内置方法来存储元素。一些常用的方法是:

1. Insert() 方法:要在数组中的指定索引处插入值,我们可以使用与数组一起使用的 insert() 方法。

句法:

例子:

在这个程序中,我们初始化了一个字符串数组。首先,我们在索引 2 处插入了一个元素,然后我们使用 insert() 方法在索引 3 处插入了一个元素。

迅速

// Swift program to demonstrate the working of
// insert() method used with arrays
 
// Initializing an array of strings
var myArray: [String] = ["Bhuwanesh", "Nainwal"]
 
// Insert at the index 2
myArray.insert("GeeksforGeeks", at:2)
 
// Print the array
print("myArray:", myArray)
 
// Insert at the index 3
myArray.insert("GFG", at:3)
 
// Print the array
print("myArray:", myArray)

输出:

myArray: ["Bhuwanesh", "Nainwal", "GeeksforGeeks"]
myArray: ["Bhuwanesh", "Nainwal", "GeeksforGeeks", "GFG"]

2. append() 方法:要在数组末尾添加值,我们可以使用 append 方法。语法如下,

句法:

例子:

迅速

// Swift program to demonstrate the working
// of append method used with an array
 
// Creating an empty array
var myArray: [String] = []
 
// Adding elements
// Using append method
myArray.append("GeeksforGeeks")
myArray.append("GFG")
myArray.append("Bhuwanesh")
myArray.append("Nainwal")
 
// Displaying result
print("myArray:", myArray)

输出:

myArray: ["GeeksforGeeks", "GFG", "Bhuwanesh", "Nainwal"]

连接数组

在 Swift 中,我们可以连接一个数组。在 (+)运算符的帮助下,可以通过连接两个具有兼容类型的现有数组来创建一个新数组。 swift 编译器根据连接在一起的两个数组的类型推断新创建的数组的类型。

句法:

例子:

迅速

// Swift program to demonstrate the working of
// (+) operator used to combine two arrays
 
// Initializing an array of integers
var myArray1: [Int] = [6, 1, 4]
 
// Initializing another array of integers
var myArray2: [Int] = [7, 3, 5, 2]
 
// Creating a new array by combining existing arrays
var newArray = myArray1 + myArray2
 
// Print the array
print("newArray:", newArray)

输出:

newArray: [6, 1, 4, 7, 3, 5, 2]

遍历数组

遍历数组意味着逐个遍历数组中存在的元素。在 Swift 中,我们可以在“for-in”循环的帮助下遍历数组中的元素。下面是遍历数组的语法,

句法:

这里,元素指向 myArray 的当前元素。

例子:

在这个程序中,我们用字符串字面量初始化了一个数组 myArray。现在我们正在迭代 myArray。这里,“element”指向 myArray 的当前元素。

迅速

// Swift program to illustrate how to
// iterate over the array
 
// Initializing an array
var myArray = ["GeeksforGeeks", "Bhuwanesh", "Nainwal"]
 
// Using for-in loop to iterate over array elements
for element in myArray{
       
      // Print the current element
    print(element)
}

输出:

GeeksforGeeks
Bhuwanesh
Nainwal

计算数组元素

在 Swift 中,我们可以借助 count 属性轻松计算数组的元素。此属性用于确定数组中存在的元素数。

句法:

返回类型:非负整数:myArray 中存在的元素数

示例:在这个程序中,我们初始化了一个整数数组,并使用“count”属性计算数组中存在的元素数。

迅速

// Swift program to demonstrate the working
// of count method used with an array
 
// Array
var myArray: [Int] = [7, 3, 5, 2]
 
// Displaying the total elements of an array
// Using count property
print("Number of elements present in myArray:", myArray.count)

输出:

Number of elements present in myArray: 4

删除数组的元素

在 Swift 中,我们可以借助 remove() 方法轻松删除数组的元素。此方法用于从数组中删除特定位置的元素。

句法:

示例:在这个程序中,我们初始化了一个字符串数组。我们已经使用“remove()”方法删除了一个特定的元素。

迅速

// Swift program to demonstrate the working
// of remove method used with arrays
 
// Initializing an array
var myArray: [String] = ["Bhuwanesh", "Nainwal",
                         "GeeksforGeeks", "Swift",
                         "Python", "Java"]
 
// Print the array
print("myArray:", myArray)
 
// Remove the element present at the index 1
myArray.remove(at: 1)
 
// Print the array
print("myArray:", myArray)
 
// Remove the element present at the index 3
myArray.remove(at: 3)
 
// Print the array
print("myArray:", myArray)

输出:

myArray: ["Bhuwanesh", "Nainwal", "GeeksforGeeks", "Swift", "Python", "Java"]
myArray: ["Bhuwanesh", "GeeksforGeeks", "Swift", "Python", "Java"]
myArray: ["Bhuwanesh", "GeeksforGeeks", "Swift", "Java"]