2016-04-04 7 views
0

分割アレイとは、私はまた、彼らの<code>index</code><code>0</code>と<code>1</code>数字とに分割を取得する必要があります上記のアレイでは私はこのような配列をしたインデックス

[ 
    1, 
    1, 
    1, 
    0, 
    0, 
    0, 
    1, 
    1, 
    0, 
    1, 
    1 
] 

を取得します。私は別の配列の01を取得するためのコードを書くが、私はメインアレイにも彼らのインデックスをしたい、コードを下回っている。そして、

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

配列はindexPathsを持っていない、ありがとうございました。索引が必要な場合は、すでに索引を持っています。ループカウンタ( 'i')です。 – Avi

+0

インデックス用に別の配列を作成したいのですか同じ配列にもインデックス0を含める必要がありますか? –

+0

同じ配列は0と1を含む必要があります –

答えて

0
NSArray *[email protected][@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); 
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])]; 

    } 
} 
関連する問題

 関連する問題