将NSMutableArray的前10个对象复制到另一个NSMutableArray

时间:2011-09-19 07:13:02

标签: objective-c ios4 nsmutablearray

我有35个项目的NSMutableArray,我想将前10个项目复制到另一个数组。

在我的数组中添加10个项目后,我想再次添加相同的10个项目。

请建议我怎么做。

代码:

我用这种方式初始化了包含所有图像的数组

    self.arrayAllAnimalImages = [[NSMutableArray alloc] init];

[self.arrayAllAnimalImages addObject:@"bagheera.jpg"];
[self.arrayAllAnimalImages addObject:@"baloo.jpg"];
[self.arrayAllAnimalImages addObject:@"bear.jpg"];
[self.arrayAllAnimalImages addObject:@"bird.jpg"];
[self.arrayAllAnimalImages addObject:@"camel.jpg"];
[self.arrayAllAnimalImages addObject:@"cock.jpg"];
[self.arrayAllAnimalImages addObject:@"deer.jpg"];
[self.arrayAllAnimalImages addObject:@"dolphin.jpg"];
[self.arrayAllAnimalImages addObject:@"donkey.jpg"];
[self.arrayAllAnimalImages addObject:@"elephant.jpg"];
[self.arrayAllAnimalImages addObject:@"fish.jpg"];
[self.arrayAllAnimalImages addObject:@"flamigo.jpg"];
[self.arrayAllAnimalImages addObject:@"frog.jpg"];
[self.arrayAllAnimalImages addObject:@"girraf.jpg"];
[self.arrayAllAnimalImages addObject:@"hippo.jpg"];
[self.arrayAllAnimalImages addObject:@"monkey.jpg"];
[self.arrayAllAnimalImages addObject:@"octopus.jpg"];
[self.arrayAllAnimalImages addObject:@"owl.jpg"];
[self.arrayAllAnimalImages addObject:@"panda.jpg"];
[self.arrayAllAnimalImages addObject:@"parrot.jpg"];
[self.arrayAllAnimalImages addObject:@"peacock.jpg"];
[self.arrayAllAnimalImages addObject:@"penguin.jpg"];
[self.arrayAllAnimalImages addObject:@"piglet.jpg"];
[self.arrayAllAnimalImages addObject:@"polarbear.jpg"];
[self.arrayAllAnimalImages addObject:@"pumba.jpg"];
[self.arrayAllAnimalImages addObject:@"rabbit.jpg"];
[self.arrayAllAnimalImages addObject:@"redpanda.jpg"];
[self.arrayAllAnimalImages addObject:@"seahorse.jpg"];
[self.arrayAllAnimalImages addObject:@"simba.jpg"];
[self.arrayAllAnimalImages addObject:@"snake.jpg"];
[self.arrayAllAnimalImages addObject:@"starfish.jpg"];
[self.arrayAllAnimalImages addObject:@"tiger.jpg"];
[self.arrayAllAnimalImages addObject:@"tigger.jpg"];
[self.arrayAllAnimalImages addObject:@"turtle.jpg"];
[self.arrayAllAnimalImages addObject:@"whale.jpg"];

我想从上面的数组中随机输入10个项目,所以这里有一个混洗数组

for (int i = 0; i<[arrayAllAnimalImages count] ;i++)
    {
        NSUInteger randomIndex = arc4random() % [arrayAllAnimalImages  count];
        [arrayAllAnimalImages  exchangeObjectAtIndex:i withObjectAtIndex:randomIndex];  


    }   

现在我希望将这10个项目添加到一个空数组中,我在这里初始化:

if (LevelCount ==2)
{
    self.arrayLevelImages= [[NSMutableArray alloc] init];

    if ([arrayLevelImages count] > 0)
    {
        [arrayLevelImages removeAllObjects];
    }

现在我要添加arrayAllAnimalImages的前10个对象。

添加前10项后,我想再次将相同的前10项添加到arrayLevelImages。

}   

请帮助!!!!!

2 个答案:

答案 0 :(得分:39)

NSArray *tempArray = [arrayAllAnimalImages subarrayWithRange:NSMakeRange(0, 10)];
[arrayLevelImages addObjectsFromArray:tempArray];
[arrayLevelImages addObjectsFromArray:tempArray];

答案 1 :(得分:0)

NSArray *arrayLevelImages = [[arrayAllAnimalImages 
                              subarrayWithRange:NSMakeRange(0, 10)] mutableCopy];