2011-06-02 99 views
1

一个或多个阵列我有一个整数数组,我想这个数组分成整数较小的阵列,然后把这些小数组到一个新的数组,洗牌这个新的数组,重新填充原始数组与洗牌整数。结果将是原始数组的混洗版本,但是每个单独的项目不会被混洗,而是原来的大块混洗。洗牌在Objective-C

我可以在Java,C#这样做等,但新的Objective-C,所以任何实例将是有益的。

这里是我迄今:

NSMutableArray *chunks = [[NSMutableArray alloc] init]; 
for(int x = 0; x < [rawData count]; x += 400){ 
    NSMutableArray *chunk = [[NSMutableArray alloc] init]; 
    for(int y = 0; y < 400; y++){ //chunks of 400 items 
     [chunk addObject:[NSNumber numberWithInt:rawData[x + y]]]; 
    } 
    count ++; 
} 
//shuffle chunks 
int index = 0; 
for(int i = 0; i < [chunks count]; i ++){ 
    for (int y = 0; y < 400; y++) { 
     //how do I put the chunks chunk back into the rawData[index]?? 
    } 
} 

回答

0

这是你在找什么(请注意,您在您的第二个循环迭代计数至400 ...你必须停止在“chunk.count “):

for(int i = 0; i < [chunks count]; i ++){ 
    NSMutableArray *chunk = [chunks objectAtIndex:i]; 
    for (int y = 0; y < [chunk count]; y++) { 
    [rawData replaceObjectAtIndex:i*400+y withObject:[chunk objectAtIndex:y]];