Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split an NSArray into two equal pieces?

I have an NSArray, and I want to split it into two equal pieces (if odd "count" then add to the latter new array) - I want to split it "down the middle" so to speak.

The following code does exactly what I want, but is there a better way?:

// NOTE: `NSArray testableArray` is an NSArray of objects from a class defined elsewhere;
NSMutableArray *leftArray = [[NSMutableArray alloc] init];  
NSMutableArray *rightArray = [[NSMutableArray alloc] init];

for (int i=0; i < [testableArray count]; i=i+1) {
if (i < [testableArray count]/2) {
        [leftArray addObject:[testableArray objectAtIndex:i]];
    }
    else {
        [rightArray addObject:[testableArray objectAtIndex:i]];
    }
}

Once leftArray and rightArray are made, I will not change them, so they do not need to be "mutable". I think there may be a way to accomplish the above code with the ObjectsAtIndexes method or some fast enumeration method?, but I cannot get the following code to work (or other variations):

NSArray *leftArray = [[NSArray alloc] initWithObjects:[testableArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(????, ????)]]];
NSArray *rightArray = [[NSArray alloc] initWithObjects:[testableArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(????, ????)]]];

Does anyone know if I am going in the right direction with this or point me in the correct direction?

Thanks!

like image 945
J. Dave Avatar asked Nov 20 '09 03:11

J. Dave


People also ask

What's a difference between NSArray and NSSet?

The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

Is NSArray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...

Can we use NSArray in Swift?

In Swift, the NSArray class conforms to the ArrayLiteralConvertible protocol, which allows it to be initialized with array literals. For more information about object literals in Swift, see Literal Expression in The Swift Programming Language (Swift 4.1).


1 Answers

You also have the option of using -subarrayWithRange: detailed in the NSArray documentation:

NSArray *firstHalfOfArray;
NSArray *secondHalfOfArray;
NSRange someRange;

someRange.location = 0;
someRange.length = [wholeArray count] / 2;

firstHalfOfArray = [wholeArray subarrayWithRange:someRange];

someRange.location = someRange.length;
someRange.length = [wholeArray count] - someRange.length;

secondHalfOfArray = [wholeArray subarrayWithRange:someRange];

This method returns new, autorelease-d arrays.

like image 135
Alex Reynolds Avatar answered Nov 07 '22 06:11

Alex Reynolds