我怎么能显示最后一个索引数组到第一个索引[重复]

时间:2022-05-02 08:31:00

This question already has an answer here:

这个问题在这里已有答案:

I have a stupid problem.

我有一个愚蠢的问题。

var array1 = ["a","b","c","d"]

I want to show it start last index to first for example

我想先显示它开始的最后一个索引

// d , c , b , a

How can I do it?

我该怎么做?

2 个解决方案

#1


1  

let numbers = [3, 5, 7]
for number in numbers.reversed() {
    print(number)
}

Take a look at Apple's documentation on that.

看一下Apple的文档。

#2


1  

that's an array. there's a reverse method on arrays.

那是一个数组。数组上有一个反向方法。

You should definitely consult a Swift reference while you're coding. Apple has a free one. Swift Manual

在编码时,你绝对应该参考Swift参考。 Apple有一个免费的。 Swift手册

var a = [1, 1, 3, 4, 5]

let b = a.reversed()
a.reverse()

at this point, a and b are both: [5, 4, 3, 1, 1]

此时,a和b都是:[5,4,3,1,1]

You can also type the array name, followed by a dot

您还可以键入数组名称,后跟一个点

array.

the IDE will prompt you with all the possibilities you have to alter the array. reverse and reversed are just two of them.

IDE将提示您更改阵列的所有可能性。反向和反向只是其中两个。

Once you settle on an option, try option-click on your selection, and you'll see a full description:

一旦您选择了某个选项,请尝试按选项单击您的选择,您将看到完整描述:

mutating func reverse()

mutating func reverse()

Description Reverses the elements of the collection in place. The following example reverses the elements of an array of characters:

说明反转集合的元素。以下示例反转了字符数组的元素:

var characters: [Character] = ["C", "a", "f", "é"]
characters.reverse()
print(cafe.characters)
// Prints "["é", "f", "a", "C"]

Complexity

O(n), where n is the number of elements in the collection.

O(n),其中n是集合中元素的数量。

That's a lot of useful information!

这是很多有用的信息!

#1


1  

let numbers = [3, 5, 7]
for number in numbers.reversed() {
    print(number)
}

Take a look at Apple's documentation on that.

看一下Apple的文档。

#2


1  

that's an array. there's a reverse method on arrays.

那是一个数组。数组上有一个反向方法。

You should definitely consult a Swift reference while you're coding. Apple has a free one. Swift Manual

在编码时,你绝对应该参考Swift参考。 Apple有一个免费的。 Swift手册

var a = [1, 1, 3, 4, 5]

let b = a.reversed()
a.reverse()

at this point, a and b are both: [5, 4, 3, 1, 1]

此时,a和b都是:[5,4,3,1,1]

You can also type the array name, followed by a dot

您还可以键入数组名称,后跟一个点

array.

the IDE will prompt you with all the possibilities you have to alter the array. reverse and reversed are just two of them.

IDE将提示您更改阵列的所有可能性。反向和反向只是其中两个。

Once you settle on an option, try option-click on your selection, and you'll see a full description:

一旦您选择了某个选项,请尝试按选项单击您的选择,您将看到完整描述:

mutating func reverse()

mutating func reverse()

Description Reverses the elements of the collection in place. The following example reverses the elements of an array of characters:

说明反转集合的元素。以下示例反转了字符数组的元素:

var characters: [Character] = ["C", "a", "f", "é"]
characters.reverse()
print(cafe.characters)
// Prints "["é", "f", "a", "C"]

Complexity

O(n), where n is the number of elements in the collection.

O(n),其中n是集合中元素的数量。

That's a lot of useful information!

这是很多有用的信息!