拆分数组并获取索引
Split an Array and get the Index
我有一个这样的数组,
[
1,
1,
1,
0,
0,
0,
1,
1,
0,
1,
1
]
在上面的数组中,我需要拆分 0
和 1
数字以及它们的 index
。我在单独的数组中编写了获取 0
和 1
的代码,但我希望它们的索引也在主数组中,下面的代码是:
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
的索引,你能帮帮我吗?谢谢
NSMutableArray *arrayContainZero = [NSMutableArray new];
NSMutableArray *arrayWithZeroIndexes = [NSMutableArray new];
for (NSNumber *numberZero in Array)
{
if(numberZero.integerValue == 0)
{
[arrayContainZero addObject:numberZero];
[arrayWithZeroIndexes addObject:@([arrayContainZeroindexOfObject:numberZero])];
}
}
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);
我有一个这样的数组,
[
1,
1,
1,
0,
0,
0,
1,
1,
0,
1,
1
]
在上面的数组中,我需要拆分 0
和 1
数字以及它们的 index
。我在单独的数组中编写了获取 0
和 1
的代码,但我希望它们的索引也在主数组中,下面的代码是:
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
的索引,你能帮帮我吗?谢谢
NSMutableArray *arrayContainZero = [NSMutableArray new];
NSMutableArray *arrayWithZeroIndexes = [NSMutableArray new];
for (NSNumber *numberZero in Array)
{
if(numberZero.integerValue == 0)
{
[arrayContainZero addObject:numberZero];
[arrayWithZeroIndexes addObject:@([arrayContainZeroindexOfObject:numberZero])];
}
}
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);