您的位置:首页 > 其它

根据一个整齐的数据,随机的得到一个新的数组,可指定数组元素出现的位置

2015-03-24 22:44 302 查看
+ (NSArray *)getRandomWithPosition:(NSInteger)position positionContent:(id)positionContent array:(NSArray *)baseArray {

NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:baseArray.count];
NSMutableArray *tempBaseArray = [NSMutableArray arrayWithArray:baseArray];

while ([tempBaseArray count]) {
NSInteger range = [tempBaseArray count];
id string = [tempBaseArray objectAtIndex:arc4random()%range];
[resultArray addObject:string];
[tempBaseArray removeObject:string];
}
// 计算指定数组元素在新数组中的位置
NSUInteger index = [resultArray indexOfObject:positionContent];
// 交换数组元素
[resultArray exchangeObjectAtIndex:index withObjectAtIndex:position];

return resultArray;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐