How to reverse array in Swift without using “.reverse()”?

后端 未结 21 1596
清歌不尽
清歌不尽 2020-12-08 20:14

I have array and need to reverse it without Array.reverse method, only with a for loop.

var names:[String] = [\"Apple\", \"Microsof         


        
相关标签:
21条回答
  • 2020-12-08 20:44

    Only need to make (names.count/2) passes through the array. No need to declare temporary variable, when doing the swap...it's implicit.

    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"]
    
    0 讨论(0)
  • 2020-12-08 20:46

    Swift 3:

    var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
    
    var reversedNames : [String] = Array(names.reversed())
    
    print(reversedNames)  // ["Asus", "Lenovo", "Sony", "Microsoft", "Apple"]
    
    0 讨论(0)
  • 2020-12-08 20:46
    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!)")
    

    A more simple way :)

    0 讨论(0)
  • 2020-12-08 20:47

    There's also stride to generate a reversed index:

    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])
    }
    

    It also works well with map:

    let reversed = (names.count - 1).stride(to: -1, by: -1).map { names[$0] }
    

    Note: stride starts its index at 1, not at 0, contrary to other Swift sequences.

    However, to anyone reading this in the future: use .reverse() instead to actually reverse an array, it's the intended way.

    0 讨论(0)
  • 2020-12-08 20:47

    Do it like this for reversed sorting.

     let unsortedArray: [String] = ["X", "B", "M", "P", "Z"]
            // reverse sorting
            let reversedArray = unsortedArray.sorted() {$0 > $1}
    
            print(reversedArray) // ["Z", "X", "P", "M", "B"]
    
    0 讨论(0)
  • 2020-12-08 20:49

    Here is @Abhinav 's answer translated to 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])
    }
    

    Using this code shouldn't give you any errors or warnings about the use deprecated of C-style for-loops or the use of --.

    Swift 3 - Current:

    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])
    }
    

    Alternatively, you could loop through normally and subtract each time:

    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])
    }
    
    0 讨论(0)
提交回复
热议问题