• Swift # 数组


    import UIKit
    
    /*
    数组: 存储一组有序的数据
    数组定义:
    >OC:
    有值数组
    NSArray *arr0 = @[@1, @2, @3];
    NSArray *arr1 = [NSArray arrayWithObjects:@1, @2, @3, nil];
    NSLog(@"%@", arr1);
    
    空数组
    NSArray *arr2 = @[];
    NSArray *arr3 = [NSArray array];
    NSLog(@"%@", arr3);
    
    不可变数组:NSArray
    可变数组:NSMutableArray
    
    >Swift:*/
    // 有值数组
    var arr0 = [1, 2, 3]
    var arr1: Array = [1, 2, 3]
    var arr2: Array<Int> = [1, 2, 3]
    var arr3: [Int] = [1, 2, 3]
    //var arr4: Int[] = [1, 2, 3] 早期写法
    
    // 空数组
    var arr5 = []
    var arr6 = [Int]()
    var arr7 = Array<Int>()
    // 带初始值的数组
    var arr8 = Array(count: 5, repeatedValue: 1)
    println(arr8)
    
    // 不可变数组:var arr0 = []
    // 可变数组:let arr0  = []
    
    
    
    /*
    元素类型:
    元素类型
    OC:
    NSArray *arr = @[@1, @"lnj", @1.75];
    NSLog(@"%@", arr);
    
    Swift:*/
    var arr_02 = [1, "lnj", 1.75]
    println(arr_02)
    
    /*
    如果想明确表示数组中存放的是不同类型的数据, 可以使用Any关键字, 表示数组中可以存放不同类型的数据
    var arr:Array<Any> = [1, "lnj", 1.75]
    println(arr)
    */
    
    /*
    数组操作
    1.获取长度
    OC:
    NSArray *arr = @[@1, @2, @3];
    NSLog(@"%tu", arr.count);
    
    Swift:
    var arr = [1, 2, 3]
    println(arr.count)
    
    2.判断是否为空
    OC:
    NSArray *arr = @[];
    NSLog(@"%d", arr.count != 0);
    
    Swift:*/
    var arr_01 = [1, 2, 3]
    println(arr_01.isEmpty)
    
    /*
    3.检索
    OC:
    NSArray *arr = @[@1, @2, @3];
    NSLog(@"%@", arr[0]);
    
    Swift:
    var arr = [1, 2, 3]
    println(arr[0])
    
    4.追加
    OC:
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr addObject:@4];
    NSLog(@"%@", arr);
    
    Swift:*/
    var arr02 = [1, 2, 3]
    arr02.append(4);
    println(arr02)
    
    var arr03 = [1, 2, 3]
    arr03 += [4]
    // arr03 += 4 以前的版本可以这样写
    //arr03 += [5, 6, 7]
    arr03 += arr03[0...1] // 还可以自己搞自己
    println(arr03)
    
    /*
    5.插入
    OC:
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr insertObject:@4 atIndex:0];
    NSLog(@"%@", arr);
    
    Swift:*/
    var arr = [1, 2, 3]
    arr.insert(4, atIndex: 0);
    println(arr)
    
    /*
    6.更新
    OC:
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    arr[0] = @8;
    NSLog(@"%@", arr);
    
    Swift:*/
    var arr01 = [1, 2, 3]
    arr01[0] = 8
    println(arr01)
    
    var arr0002 = [1, 2, 3]
    arr0002[0..<2] = [8, 9]
    println(arr0002)
    
    /*
    7.删除
    OC:
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr removeObject:@1];
    NSLog(@"%@", arr);
    
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr removeLastObject];
    NSLog(@"%@", arr);
    
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr removeAllObjects];
    NSLog(@"%@", arr);
    
    Swift:*/
    var arr001 = [1, 2, 3]
    arr001.removeAtIndex(0)
    println(arr001)
    
    var arr002 = [1, 2, 3]
    arr002.removeLast()
    println(arr002)
    
    var arr003 = [1, 2, 3]
    arr003.removeAll(keepCapacity: false) //是否保持容量, 如果为true, 即便删除了容量依然存在, 容量是2的倍数
    println(arr003)
    println(arr003.capacity)
    /*
    注意: 如果数组是一个不可变数组不能更新/插入和删除
    第一个版本的不可变数组是可以修改的
    */
    
    /*
    Range
    OC:
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr removeObjectsInRange:NSMakeRange(0, 2)];
    NSLog(@"%@", arr);
    
    Swift:*/
    var arr_1 = [1, 2, 3]
    arr_1.removeRange(Range(start: 1, end: 2))
    println(arr_1)
    
    var arr_2 = [1, 2, 3]
    arr_2.removeRange(0...0)
    println(arr_2)
    
    // 其实Range就是半闭区间
    var range = 0...5
    //range = 99 // 通过报错可以推断出类型
    println(range) // 通过打印也可以推断出类型
    
    var range1:Range<Int> = 0...5
    //var range2:Range<String>; // 必须遵守ForwardIndexType协议
    // start 起点 end 终点
    var range3:Range<Int> = Range(start: 0, end: 5)
    var range4:Range<Int> = 0..<5
    println(range1)
    println(range3)
    println(range4)
    
    /*
    OC:
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    NSRange range = NSMakeRange(0, 2);
    // [arr replaceObjectsInRange:range withObjectsFromArray:@[@99, @88]];
    [arr replaceObjectsInRange:range withObjectsFromArray:@[@99, @88, @77, @66]];
    NSLog(@"%@", arr);
    
    Swift:*/
    //arr.replaceRange(Range(start: 0, end: 2), with: [99, 88, 77, 66])
    arr.replaceRange(1..<2, with: [99, 88, 77, 66])
    println(arr)
    
    // 等价于上一行代码
    var arr_3 = [1, 2, 3]
    arr_3[Range(start: 0, end: 2)] = [8, 9]
    println(arr_3)
    
    // 等价于上一行代码
    var arr_4 = [1, 2, 3]
    //arr[0...1] = [99, 88]
    //arr[0...1] = [99, 88, 77, 66]
    
    
    
    println("====--遍历--====");
    /*
    4.遍历
    OC:
    // for循环
    NSArray *arr = @[@1, @2, @3];
    for (int i = 0; i < arr.count; i++) {
    NSLog(@"%@", arr[i]);
    }
    
    // for in循环
    for (NSNumber *number in arr) {
    NSLog(@"%@", number);
    }
    
    // 迭代器
    NSArray *arr0 = @[@1, @2, @"1"];
    [arr0 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"index = %tu element = %@", idx, obj);
    }];
    
    Swift:*/
    // for循环
    var arr_a = [1, 2, 3]
    for var i = 0 ; i < arr_a.count ; i++
    {
    println(arr_a[i])
    }
    
    // for in循环
    for number in arr_a
    {
    println(number)
    }
    
    for i in 0..<arr_a.count
    {
    println(arr_a[i])
    }
    
    // 取出数组中某个区间范围的值
    var arr_b = [1, 2, 3]
    for number in arr_b[0..<3]
    {
    println(number)
    }
    
    // 利用enumerate迭代
    var arr_c = [1, 2, 3]
    for (index , value) in enumerate(arr_c)
    {
    println("index = (index) value = (value)")
    }

    |--> Copyright (c) 2015 Bing Ma.

    |--> GitHub RUL: https://github.com/SpongeBob-GitHub



    " There's always more to learn, and there are always better ways to do what you've done before. " -- Trybst


  • 相关阅读:
    Codeforces D
    Codeforces C
    Minimal Ratio Tree HDU
    Tian Ji -- The Horse Racing HDU
    Monkey Banana Problem LightOJ
    Rooks LightOJ
    洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows || 凸包模板
    洛谷 P3382 【模板】三分法
    洛谷 P1438 无聊的数列
    洛谷 P1082 同余方程
  • 原文地址:https://www.cnblogs.com/Trybst/p/4495664.html
Copyright © 2020-2023  润新知