我是这样的数组,
[
1,
1,
1,
0,
0,
0,
1,
1,
0,
1,
1
]
在上面的数组中,我需要将0
和1
数字以及index
分开。我编写了将0
和1
放在单独数组中的代码,但我希望它们的索引也在Main数组中,代码如下:
for (int i = 0; i < [Array count]; i++) {
NSString* strV = [Array objectAtIndex:i];
NSLog(@"ArrayCount:%@",strV);
if ([strV isEqual:[NSNumber numberWithInt:0]]) {
[getArray addObject:strV];
}
}
NSLog(@"getArray:%@",getArray.description);
}
然后,我怎样才能得到0
的索引,请你帮忙。谢谢
答案 0 :(得分:0)
NSMutableArray *arrayContainZero = [NSMutableArray new];
NSMutableArray *arrayWithZeroIndexes = [NSMutableArray new];
for (NSNumber *numberZero in Array)
{
if(numberZero.integerValue == 0)
{
[arrayContainZero addObject:numberZero];
[arrayWithZeroIndexes addObject:@([arrayContainZeroindexOfObject:numberZero])];
}
}
答案 1 :(得分:0)
NSArray *mainArr=@[@1,@1,@1,@0,@0,@0,@1,@1,@0,@1,@1];
NSMutableArray *tempArr=[[NSMutableArray alloc] init];
for(int i=0;i<[mainArr count];i++){
if([[mainArr objectAtIndex:i] isEqualToNumber:[NSNumber numberWithInt:0]]){
NSMutableArray *arr=[[NSMutableArray alloc] initWithArray:tempArr];
[tempArr removeAllObjects];
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"value",[NSNumber numberWithInt:i],@"index",nil];
[tempArr addObject:dic];
[tempArr addObjectsFromArray:arr];
}else if ([[mainArr objectAtIndex:i] isEqualToNumber:[NSNumber numberWithInt:1]]){
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1],@"value",[NSNumber numberWithInt:i],@"index",nil];
[tempArr addObject:dic];
}
}
NSLog(@"%@",tempArr.description);