iOS开发- 快速将数组 分成指定最大数量小数组

 //创建大数组
    NSMutableArray *allNumberAry = [NSMutableArray arrayWithCapacity:1000];
    for (NSInteger idx = 0; idx < 39482; idx ++)
    {
        [allNumberAry addObject:[NSNumber numberWithInteger:idx]];
    }
    
    
    if (allNumberAry.count > 0)
    {
        //每组最大数量
        NSInteger oneGroupCount = 100;
        //组数
        NSInteger groupCount = (allNumberAry.count - 1) / oneGroupCount + 1;
        //前面每组达到最大数量之后,剩余不足最大数量的元素个数
        NSInteger surplusCount = allNumberAry.count % oneGroupCount;
        
        
        for (NSInteger group = 0; group < groupCount; group ++)
        {
            //先假设每组都可以放最大元素数量
            NSRange range = NSMakeRange(group * oneGroupCount, oneGroupCount - 1);
            //假如剩余个数大于0,则需要添加一组盛放剩下的元素
            if (surplusCount > 0 &&
                group == groupCount - 1)
            {
                //将范围修改
                range = NSMakeRange(group * oneGroupCount, surplusCount);
            }
            
            NSArray *subAry = [allNumberAry subarrayWithRange:range];
            NSLog(@"%ld",subAry.count);
        }
    }
NSMutableArray *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 *>*pageAry = [NSMutableArray arrayWithCapacity:pageCount + 1];
    
    for (NSInteger idx = 0; idx < pageCount; idx ++)
    {
        NSIndexSet *Indexes = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(idx * everyPage, everyPage)];
        NSArray *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 *plusAry = [muary objectsAtIndexes:Indexes];
    [pageAry addObject:plusAry];

你可能感兴趣的:(iOS开发- 快速将数组 分成指定最大数量小数组)