2012-03-26 5 views
1

私は現在、この方法があります:配列を動的に多くの配列に分ける? OBJ-C

-(void)seperateGuides 
{ 
    DLog("GetGuideListCount: %i", [[appDelegate getGuideList] count]); 

    columnOneArray = [[NSMutableArray alloc] init]; 
    columnTwoArray = [[NSMutableArray alloc] init]; 
    columnThreeArray = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < [[appDelegate getGuideList] count]; i = i + 3) { 
     [columnOneArray addObject:[[appDelegate getGuideList]objectAtIndex:i]]; 
    } 

    for (int i = 1; i < [[appDelegate getGuideList] count]; i = i + 3) { 
     [columnTwoArray addObject:[[appDelegate getGuideList]objectAtIndex:i]]; 
    } 

    for (int i = 2; i < [[appDelegate getGuideList] count]; i = i + 3) { 
     [columnThreeArray addObject:[[appDelegate getGuideList]objectAtIndex:i]]; 
    } 
} 

を、これはよりダイナミック行う必要があるので、私は私が望むどのように多くの配列を定義して、配列を得ることができます。

さまざまな可能性私が考えているのは多次元配列です(ただし、Objectvie-Cでどのように扱うかはわかりません)。あるいは、定義した回数だけループするメソッドを作成すると、問題は、私はどのように異なる配列を取得するかについてはあまりよく分かりません。

単純なアルゴリズムまたは別の可能な解決策が非常に高く評価されるでしょう。

答えて

3

我々は、複数の手にデッキのカードを扱うときは、私たちが何をすべきかと同等の音を記述しているので、私はこのようにそれを行うだろうアルゴリズム:

- (NSArray *)dealObjects:(NSArray *)objects intoArrays:(NSInteger)numArrays 
{ 
    NSMutableArray *arrays = [NSMutableArray arrayWithCapacity:numArrays]; 
    for (a = 0; a < numArrays; a++) { 
     [arrays addObject:[NSMutableArray arrayWithCapacity:[objects count]/numArrays]; 
    } 
    for (i = 0; i < [objects count]; i++) { 
     [[arrays objectAtIndex:i % numArrays] addObject:[objects objectAtIndex:i]]; 
    } 
    return arrays; 
} 
0

あなたは実際にはかなり簡単で、このよう別の配列に

-(void)seperateGuides:(int)columnCount 
{ 


    rootArray=[[NSMutableArray alloc] init]; 
    for(int i=0;i<columnCount;i++) 
    { 
    column = [[NSMutableArray alloc] init]; 
    for(int j=i;j<[[appDelegate getGuideList] count];j=j+3) 
    { 

     [column addObject:[[appDelegate getGuideList]objectAtIndex:j]]; 
     [rootArray addObject:column]; 
     [column release]; 
    } 
    } 
} 
0

を複数のアレイを追加することができます。データセットを反復処理するときに、新しいNSArrayオブジェクトをルート配列に追加するだけです。

- (void)seperateGuides { 
    DLog("GetGuideListCount: %i", [[AppDelegate getGuideList] count]); 

    NSArray *root = [[NSArray alloc] init]; 
    int dimensions = anyIntGreaterThanZero; 

    for (int i = 0; i < dimensions; i += dimensions) { 
     NSArray *branch = [[NSArray alloc] init]; 
     int k = 0; 
     for (k += i; k < [[AppDelegate getGuideList] count]; k += dimensions) { 
      [branch addObject:[[AppDelegate getGuideList] objectAtIndex:k]]; 
     } 
     [root arrayByAddingObject:branch]; 
    } 
} 
0
-(void)seperateGuides 
{ 
    NSMutableArray *parentArray = [[NSMutableArray alloc]init]; 

    int count = [[appDelegate getGuideList] count]; 
    int TOTAL_COLUMNS = 3;//Define number of columns here 
    for (int i = 0; i <count; i++) 
    { 
      int columnNo = i % TOTAL_COLUMNS; 

      if(parentArray.count > columnNo) 
      { 
       NSMutableArray *innerArray = [parentArray objectAtIndex:columnNo]; 
       [innerArray addObject:[[appDelegate getGuideList]objectAtIndex:i]]; 
      } 
      else 
      { 
       NSMutableArray *innerArray = [NSMutableArray arrayWithObject:[[appDelegate getGuideList]objectAtIndex:i]]; 
       [parentArray insertObject:innerArray atIndex:columnNo]; 
      } 
    } 
} 

・ホープこれは役に立ちます...

ここで、parentArrayはメンバーとしてNSMutableArrayを持ちます。各配列は、列内のオブジェクトを表します。

関連する問題