Hor*_*rza 1 arrays sorting swift
为什么我制作一个数组并打印它,用不同的顺序打印它
创作:
var array = [Int]();
array = [8, 1, 3, 4, 6, 5, 4, 7, 11 , 2, 10, 9];
Run Code Online (Sandbox Code Playgroud)
打印方式:
for i in array {
print(array[i], terminator: ", ");
}
Run Code Online (Sandbox Code Playgroud)
输出:
11, 1, 4, 6, 4, 5, 6, 7, 9, 3, 10, 2,
Run Code Online (Sandbox Code Playgroud)
根据定义,数组是稳定排序的.事情按照他们进入的顺序排列.
在这种情况下,您将使用数组中的值作为索引来迭代数组:
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
}
Run Code Online (Sandbox Code Playgroud)
如果您只想打印数组的元素,而不使用双索引,请使用:
for i in array {
print(i, terminator: ", ")
}
Run Code Online (Sandbox Code Playgroud)