2

How do you split an array of objects into an array of array of objects?

say I want to split into groups of 4, how do I do that?

[a,b,c,d,e,f,g,h] => [a,b] [c,d] [e,f] [g,h]

or maybe if I specify that I want to split into groups of 3, then the result should be [a,b,c], [d,e,f], [g,h]

it should also work if h doesn't exist.

3 Answers 3

2

Try this logic.....

NSArray *arr = [[NSArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",nil];
NSMutableArray *arrNew = [[NSMutableArray alloc]init];
int numberofSubArrs = 3; // change this to check the logic
for (int i=0; i<numberofSubArrs; i++) {
    NSMutableArray *arrrr = [[NSMutableArray alloc]init];
    [arrNew addObject:arrrr];
}
int m = 0;
for (int k=0; k<[arr count]; k++) {
    [[arrNew objectAtIndex:m]addObject:[arr objectAtIndex:k]];
    m++;
    if (m == numberofSubArrs) {
        m=0;
    }
}
int g=0;
int p=0;
while(p<[arr count]) {
    for (int z=0; z<[[arrNew objectAtIndex:g] count]; z++) {
        [[arrNew objectAtIndex:g] replaceObjectAtIndex:z withObject:[arr objectAtIndex:p++]];
    }
    g++;
}
NSLog(@"Required Array is:%@",[arrNew description]);
Sign up to request clarification or add additional context in comments.

1 Comment

have you actually tried this? where do you specify how many names you want to split into per array into the main array
1

Try this as a starting point:

NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", nil];
NSMutableArray *manyArrays = [NSMutableArray array];
int numberOfElementsInSubArrays = 2;
int numberOfSubArrays = ceil((float)[array count] / (float)numberOfElementsInSubArrays);
for (int i = 0; i < numberOfSubArrays; i++) {
    NSMutableArray *subArray = [NSMutableArray array];
    for (int j = 0; j < numberOfElementsInSubArrays; j++) {
        if (i*numberOfElementsInSubArrays+j < [array count]) {
            NSLog(@"Array: %d Value:%@", i, [array objectAtIndex:i*numberOfElementsInSubArrays+j]);
            [subArray addObject:[array objectAtIndex:i*numberOfElementsInSubArrays+j]];
        }
    }
    [manyArrays addObject:subArray];
}

2 Comments

something similar, but instead of setting of number of subarrays, only just specifying the number of elements in a group
try it now, I change it to set the number of elements in a group instead of the number of groups
0

Give this a try.

NSMutableArray *splitted = [NSMutableArray array];    
id firstItem, secondItem;
for (NSInteger i=0; i <= [originalArray count]-1; i+=2) {
        @try {
            firstItem = [originalArray objectAtIndex:i];
            secondItem = [originalArray objectAtIndex:i+1];
        }
        @catch (NSException *exception) {
            secondItem = [NSNull null];
        }
        @finally {
            [splitted addObject:@[firstItem,secondItem]];
        }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.