美文网首页
原来数组可以在遍历的时候添加或删除

原来数组可以在遍历的时候添加或删除

作者: LV大树 | 来源:发表于2019-12-31 10:39 被阅读0次

    //
    // ViewController.m
    // arrayfundemo
    //
    // Created by tql on 2019/12/30.
    // Copyright © 2019 tqlLW. All rights reserved.
    //

    import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
    NSMutableArray *muarray = [NSMutableArray array];
    
    for (int i = 0; i<20; i++) {
        [muarray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    NSLog(@"%@",muarray);
    
    
    for (int i = 0; i<muarray.count; i++) {
        NSString *item = muarray[i];
        
        if (item.intValue%4==0&&i>0) {
            [muarray removeObjectAtIndex:i];
            i--;
        }
        
    }
    NSLog(@"====\n%@",muarray);
    
    
    
    for (int  i  = 0; i<muarray.count; i++) {
        if (i%4==0&&i>0) {
    

    // [muarray addObject:[NSString stringWithFormat:@"%d",i+1]];
    [muarray insertObject:[NSString stringWithFormat:@"%d",i] atIndex:i+1];
    }
    i++;
    }
    NSLog(@"====\n%@",muarray);

    }

    @end

    //console

    2019-12-31 10:38:18.449872+0800 arrayfundemo[26077:1307573] (
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19
    )
    2019-12-31 10:38:18.450031+0800 arrayfundemo[26077:1307573] ====
    (
    0,
    1,
    2,
    3,
    5,
    6,
    7,
    9,
    10,
    11,
    13,
    14,
    15,
    17,
    18,
    19
    )
    2019-12-31 10:38:18.450138+0800 arrayfundemo[26077:1307573] ====
    (
    0,
    1,
    2,
    3,
    5,
    4,
    6,
    7,
    9,
    8,
    10,
    11,
    13,
    12,
    14,
    15,
    17,
    16,
    18,
    19
    )

    相关文章

      网友评论

          本文标题:原来数组可以在遍历的时候添加或删除

          本文链接:https://www.haomeiwen.com/subject/zpkboctx.html