美文网首页
将一个大数组拆封成若干个小数组

将一个大数组拆封成若干个小数组

作者: CocoaJason | 来源:发表于2017-12-07 09:54 被阅读11次

    NSMutableArray <NSString >muary = [NSMutableArray arrayWithCapacity:10];
    for (NSInteger idx = 0; idx < 101; idx ++)
    {
    [muary addObject:[NSString stringWithFormat:@"%ld",idx]];
    }

    NSInteger everyPage = 100;
    NSInteger pageCount = muary.count / everyPage;
    NSMutableArray <NSArray <NSString *>*>*pageAry = [NSMutableArray arrayWithCapacity:pageCount + 1];
    
    for (NSInteger idx = 0; idx < pageCount; idx ++)
    {
        NSIndexSet *Indexes = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(idx * everyPage, everyPage)];
        NSArray <NSString *>*subAry = [muary objectsAtIndexes:Indexes];
        NSLog(@"%@",subAry);
        [pageAry addObject:subAry];
    }
    
    NSInteger surplus = muary.count % everyPage;
    NSIndexSet *Indexes = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(muary.count - surplus, surplus)];
    NSArray <NSString *>*plusAry = [muary objectsAtIndexes:Indexes];
    [pageAry addObject:plusAry];

    相关文章

      网友评论

          本文标题:将一个大数组拆封成若干个小数组

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