已知:
strArr = [NSArray arrayWithObjects: @"a", @"b", @"c", @"d", @"e", nil];
求解:
strArr的反序数组strRevArr,即元素顺序分别为e,d,c,b,a。(这个逆序的意思只是针对数组最初元素顺序的反序,并非 对其元素进行大小或者ASCII码排序再逆序等等。)
- 方案1
NSMutableArray strRevArray = [NSMutableArray array];
int size = [strArr count];
int i;
if(size>1){
i = size -1;
}else{
i = 0;
}
for(i; i >=0; i--) {
NSString *str = [strArr objectAtIndex:i];
[strRevArray addObject:str];
}
- 方案2
NSArray *strRevArray = [[strArr reverseObjectEnumerator] allObjects];
- 方案3
NSMutableArray strRevArray = [NSMutableArray array];
for (NSString *str in [strArr reverseObjectEnumerator]) {
[strRevArray addObject:str];
}
- 方案4
NSMutableArray strRevArray = [NSMutableArray array];
[strArr.reverseObjectEnumerator.allObjects enumerateObjectsUsingBlock:^(NSString _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[strRevArray addObject:str];
}];
网友评论