我有数组,需要在没有Array.reverse
方法的情况下将其反转,只需使用for
循环。
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
答案 0 :(得分:11)
这是@Abhinav的回答转换为 Swift 2.2 :
var names: [String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for arrayIndex in (names.count - 1).stride(through: 0, by: -1) {
reversedNames.append(names[arrayIndex])
}
使用此代码不应该为您提供有关C风格for循环弃用或--
使用的任何错误或警告。
Swift 3 - 当前:
let names: [String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for arrayIndex in stride(from: names.count - 1, through: 0, by: -1) {
reversedNames.append(names[arrayIndex])
}
或者,您可以正常循环并每次减去:
let names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
let totalIndices = names.count - 1 // We get this value one time instead of once per iteration.
var reversedNames = [String]()
for arrayIndex in 0...totalIndices {
reversedNames.append(names[totalIndices - arrayIndex])
}
答案 1 :(得分:8)
Swift 3:
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames : [String] = Array(names.reversed())
print(reversedNames) // ["Asus", "Lenovo", "Sony", "Microsoft", "Apple"]
答案 2 :(得分:7)
¿loop
是必须的吗?
如果没有,您可以使用reduce
。
我猜这是没有 reverse()方法实现它的最短路径( Swift 3.0.1 ):
["Apple", "Microsoft", "Sony", "Lenovo", "Asus"].reduce([],{ [$1] + $0 })
答案 3 :(得分:4)
var names:[String] = [ "A", "B", "C", "D", "E","F","G"]
var c = names.count - 1
var i = 0
while i < c {
swap(&names[i++],&names[c--])
}
答案 4 :(得分:3)
你走了:
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for var arrayIndex = names.count - 1 ; arrayIndex >= 0 ; arrayIndex-- {
reversedNames.append(names[arrayIndex])
}
答案 5 :(得分:2)
还有stride
生成反向索引:
let names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversed = [String]()
for index in (names.count - 1).stride(to: -1, by: -1) {
reversed.append(names[index])
}
它也适用于map
:
let reversed = (names.count - 1).stride(to: -1, by: -1).map { names[$0] }
注意:stride
的索引为1,而不是0,与其他Swift序列相反。
但是,对于将来阅读此内容的任何人:使用.reverse()
代替实际反转数组,这是预期的方式。
答案 6 :(得分:2)
这里是swift 3的代码
let array = ["IOS A", "IOS B", "IOS C"]
for item in array.reversed() {
print("Found \(item)")
}
答案 7 :(得分:2)
只需要make(names.count / 2)通过数组。在进行交换时,无需声明临时变量......它是隐含的。
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
let count = names.count
for i in 0..<count/2 {
(names[i],names[count - i - 1]) = (names[count - i - 1],names[i])
}
// Yields: ["Asus", "Lenovo", "Sony", "Microsoft", "Apple"]
答案 8 :(得分:1)
像这样,也许:
names = names.enumerate().map() { ($0.index, $0.element) }.sort() { $0.0 > $1.0 }.map() { $0.1 }
哦,等等..我有使用for
循环,对吗?那么可能就是这样:
for (index, name) in names.enumerate().map({($0.index, $0.element)}).sort({$0.0 > $1.0}).map({$0.1}).enumerate() {
names[index] = name
}
答案 9 :(得分:1)
忽略对空虚的检查..
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames: [String]
reversedNames = []
for name in names {
reversedNames.insert((name), atIndex:0)
}
答案 10 :(得分:1)
var names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var newNames = [String]()
for var i = names.count - 1; i >= 0 ; i-- {
newNames.append(names[i])
}
names = newNames
或
names.map {newNames.insert($0, atIndex: 0)}
names = newNames
答案 11 :(得分:0)
我是这样做的,并没有对Swift 3的警告
let names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for name in names.enumerate() {
let newIndex = names.count - 1 - name.index
reversedNames.append(names[newIndex])
}
或只是简单地
reversedNames = names.reverse()
答案 12 :(得分:0)
这适用于任何大小的数组。
import Cocoa
var names:[String] = [ "A", "B", "C", "D", "E","F"]
var c = names.count - 1
for i in 0...(c/2-1) { swap(&names[i],&names[c-i]) }
print(names)
答案 13 :(得分:0)
func reverse(array: inout [String]) {
if array.isEmpty { return }
var f = array.startIndex
var l = array.index(before: array.endIndex)
while f < l {
swap(array: &array, f, l)
array.formIndex(after: &f)
array.formIndex(before: &l)
}
}
private func swap( array: inout [String], _ i: Int, _ j: Int) {
guard i != j else { return }
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
或者你可以写课程的扩展
答案 14 :(得分:0)
var rArray : [Int] = [2,5,6,8,3,8,9,10]
var ReArray = [Int]()
var a : Int = 1
func reversed (_ array: [Int]) -> [Int] {
for i in array {
ReArray.append(array[array.count-a])
a += 1
}
rArray = ReArray
return rArray
}
reversed(rArray)
print(rArray)
答案 15 :(得分:0)
您可以使用swift3文档:
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
let reversedNames = names.sorted(by: >)
// reversedNames is equal to:
// ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
答案 16 :(得分:0)
执行此操作以进行反向排序。
let unsortedArray: [String] = ["X", "B", "M", "P", "Z"]
// reverse sorting
let reversedArray = unsortedArray.sorted() {$0 > $1}
print(reversedArray) // ["Z", "X", "P", "M", "B"]
答案 17 :(得分:0)
这是最简单的方法。
let names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for name in names {
reversedNames.insert(name, at: 0)
}
答案 18 :(得分:0)
x_names = ['John', 'Jill', 'Sandy']
y_ages = 8,11,10
Trace00 = dict(type = 'bar', x=x_names, y= y_ages)
plotly.offline.iplot([Trace00])
答案 19 :(得分:0)
var arr = [1, 2, 3, 4, 5] // Array we want to reverse
var reverse: [Int]! // Array where we store reversed values
reverse = arr
for i in 0...(arr.count - 1) {
reverse[i] = arr.last! // Adding last value of original array at reverse[0]
arr.removeLast() // removing last value & repeating above step.
}
print("Reverse : \(reverse!)")
更简单的方法:)
答案 20 :(得分:0)
我喜欢简单的代码。
变量名:[String] = [“ Apple”,“ Microsoft”,“ Sony”,“ Lenovo”,“ Asus”]
var reversedNames = [""]
for name in names {
reversedNames.insert(name, at: 0)
}
print(reversedNames)
答案 21 :(得分:0)
Swift 5:
let names: [String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversenames: [String] = []
让计数 = names.count
用于索引在 0.. } 打印(反名)reversenames.insert(names[count-index-1], at: index)