Swift – 数组属性
数组是最常用的数据类型之一,因为它在更有效地编写程序方面提供了好处。就像其他编程语言一样,在 Swift 语言中,数组也是相似数据类型的集合。它将数据存储在订单列表中。当您将数组分配给变量时,您的变量是可变的,这意味着您可以删除、添加或修改数组中存在的元素。当您将数组分配给常量时,您的数组将是不可变的,这意味着您无法修改和更改数组的大小。在了解数组的属性之前,首先我们了解如何创建数组:
var myarray = [TypeOfArray](count: NumbeOfItems, repeatedValue: Value)
或者
var myarray:[Int] = [29, 17, 10]
扳手
var myarray:Array = Array()
例子:
var language = ["C", 'language","Java"]
var num = [Int](count: 5, repeatedValue: 0)
现在一些常用的数组属性有:
- Array.count 属性
- Array.first 属性
- Array.last 属性
- Array.isEmpty 属性
计数属性
count 属性用于计算给定数组中存在的数字或字符串的元素总数。
句法:
Array.count
例子:
Swift
// Swift program to illustrate the use of count property// Creating and initializing array variableslet myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]let myArray2:[String] = [“C++”, “Java”, “Python”, “Perl”, “Go”]let myArray3:[Int] = [3, 45, 3, 86, 50]// Using count property to count the// elements present in each arraylet res1 = myArray1.countlet res2 = myArray2.countlet res3 = myArray3.count// Displaying the total countprint(“Total elements present in first array:\(res1)”)print(“Total elements present in second array:\(res2)”)print(“Total elements present in third array:\(res3)”)
Swift
// Swift program to illustrate the use of first property// Creating and initializing array variableslet myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]let myArray2:[String] = [“C++”, “Java”, “Python”, “Perl”, “Go”]let myArray3:[Int] = [3, 45, 3, 86, 50]// Using first property to// find the first element// of the given arraylet res1 = myArray1.firstlet res2 = myArray2.firstlet res3 = myArray3.first// Displaying the first elementprint(“First element of the first array:\(res1)”)print(“First element of the second array:\(res2)”)print(“First element of the third array:\(res3)”)
Swift
// Swift program to illustrate the use of last property// Creating and initializing array variableslet myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]let myArray2:[String] = [“C++”, “Java”, “Python”, “Perl”, “Go”]let myArray3:[Int] = [3, 45, 3, 86, 50]// Using last property to find the last element// of the given arraylet res1 = myArray1.lastlet res2 = myArray2.lastlet res3 = myArray3.last// Displaying the last elementprint(“Last element of the first array:\(res1)”)print(“Last element of the second array:\(res2)”)print(“Last element of the third array:\(res3)”)
Swift
// Swift program to illustrate the use of isEmpty property// Creating and initializing array variableslet myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]let myArray2:[String] = [“C++”, “Java”, “Python”, “Perl”, “Go”]let myArray3:[Int] = [Int]()// Using isEmpty property to check// whether the given array is empty or notlet res1 = myArray1.isEmptylet res2 = myArray2.isEmptylet res3 = myArray3.isEmpty// Displaying the resultprint(“Is the first array is empty?:\(res1)”)print(“Is the second array is empty?:\(res2)”)print(“Is the third array is empty?:\(res3)”)
输出:
Total elements present in first array:7
Total elements present in second array:5
Total elements present in third array:5
第一个属性
first 属性用于查找指定数组中存在的第一个元素,无论是数字还是字符串。
句法:
Array.first
例子:
迅速
// 创建和初始化数组变量
让 myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]
让 myArray2:[String] = [“C++”、“Java”、“Python”、“Perl”、“Go”]
让 myArray3:[Int] = [3, 45, 3, 86, 50]
// 使用第一个属性
// 找到第一个元素
//给定数组的
让 res1 = myArray1.first
让 res2 = myArray2.first
让 res3 = myArray3.first
// 显示第一个元素
print(“第一个数组的第一个元素:\(res1)”)
print(“第二个数组的第一个元素:\(res2)”)
print(“第三个数组的第一个元素:\(res3)”)
输出:
First element of the first array:Optional(23)
First element of the second array:Optional("C++")
First element of the third array:Optional(3)
最后一个属性
last 属性用于查找指定数组中存在的最后一个元素,无论是数字还是字符串。
句法:
Array.last
例子:
迅速
// 创建和初始化数组变量
让 myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]
让 myArray2:[String] = [“C++”、“Java”、“Python”、“Perl”、“Go”]
让 myArray3:[Int] = [3, 45, 3, 86, 50]
// 使用 last 属性查找最后一个元素
//给定数组的
让 res1 = myArray1.last
让 res2 = myArray2.last
让 res3 = myArray3.last
// 显示最后一个元素
print(“第一个数组的最后一个元素:\(res1)”)
print(“第二个数组的最后一个元素:\(res2)”)
print(“第三个数组的最后一个元素:\(res3)”)
输出:
Last element of the first array:Optional(34)
Last element of the second array:Optional("Go")
Last element of the third array:Optional(50)
isEmpty 属性
isEmpty 属性用于查找指定的数组是否为空。如果指定的数组为空,此属性将返回 true。否则,如果指定的数组不为空,它将返回 false。
句法:
Array.isEmpty
例子:
迅速
// 创建和初始化数组变量
让 myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]
让 myArray2:[String] = [“C++”、“Java”、“Python”、“Perl”、“Go”]
让 myArray3:[Int] = [Int]()
// 使用 isEmpty 属性检查
// 给定的数组是否为空
让 res1 = myArray1.isEmpty
让 res2 = myArray2.isEmpty
让 res3 = myArray3.isEmpty
// 显示结果
print(“第一个数组是空的吗?:\(res1)”)
print(“第二个数组是否为空?:\(res2)”)
print(“第三个数组是否为空?:\(res3)”)
输出:
Is the first array is empty?:false
Is the second array is empty?:false
Is the third array is empty?:true