gpt4 book ai didi

arrays - Swift 中的数组有顺序吗?

转载 作者:搜寻专家 更新时间:2023-11-01 05:47:35 24 4
gpt4 key购买 nike

为什么如果我制作一个数组并打印它,以不同的顺序打印它

创作:

var array = [Int]();

array = [8, 1, 3, 4, 6, 5, 4, 7, 11 , 2, 10, 9];

打印方法:

for i in array  {  

print(array[i], terminator: ", ");

}

输出:

11, 1, 4, 6, 4, 5, 6, 7, 9, 3, 10, 2, 

最佳答案

根据定义,数组是稳定排序的。事物出现的顺序与它们进入的顺序相同。

在这种情况下,您将遍历数组,使用数组中的值作为索引:

for i in array {      // this line fetches each element from array in order,
// ie., 8, 1, 3, 4, 6, ...
print(array[i]...) // this line indexes into array using the value you
// just fetched
}

如果你只是想打印数组的元素,没有双索引,使用:

for i in array {
print(i, terminator: ", ")
}

关于arrays - Swift 中的数组有顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35998216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com