2014-03-05 51 views
-4

我有三個NSMutabelArrays,分別包含大約十串,例如:iphone:如何使對象的數組,從其他陣列

1stArray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",nil]; 

    2ndArray = [NSMutableArray arrayWithObjects:@"j",@"k",@"l",@"m",@"n",@"o",@"p",nil]; 

    3rdArray = [NSMutableArray arrayWithObjects:@"q",@"r",@"s",@"t",@"u",@"v",@"w",nil]; 

現在我想和那些第一次2個對象第4 NSMutabelArrays 3 NSMutableArray。 比如我想一個4陣列是這樣的:

4thArray = [@"a",@"b",@"j",@"k",@"q",@"r"] 

如何實現這一目標?

+2

[NSArray的類參考(https://developer.apple.com/library/ios/代碼文檔/ Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html),[NSMutableArray Class Reference](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference /Reference.html) – Desdenova

+0

不會使用第一,第二,第三個 – NullData

回答

1

我認爲你正在尋找這樣

int numElements = 2; 
int numArrays = 3; 

NSMutableArray * 4thArray = [NSMutableArray array]; 

for (int arrays = 0; arrays < numArrays; arrays++) { 
    for (int count = 0; count < numElements; count++) { 
     switch (arrays) { 
      case 0: 
       [4thArray addObject:[1stArray objectAtIndex:count]]; 
       break; 
      case 1: 
       [4thArray addObject:[2ndArray objectAtIndex:count]]; 
       break; 
      case 2: 
       [4thArray addObject:[3rdArray objectAtIndex:count]]; 
       break; 
     } 
    } 
} 
+0

謝謝。這正是我正在尋找的東西。 –

0

看起來像這樣會更好地解決使用陣列的數組,或可能是數組和字典的組合,這取決於你想要實現什麼。你的問題的答案是微不足道的,所以我會爲你指出NSArray Reference,但你可能想考慮一個不同的方法。

0

做這樣

NSMutableArray *firstArray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",nil]; 
    NSMutableArray *secArray = [NSMutableArray arrayWithObjects:@"j",@"k",@"l",@"m",@"n",@"o",@"p",nil]; 
    NSMutableArray *thrdArray = [NSMutableArray arrayWithObjects:@"q",@"r",@"s",@"t",@"u",@"v",@"w",nil]; 

    NSMutableArray *fourthArr = [[NSMutableArray alloc] initWithObjects:[firstArray objectAtIndex:0],[firstArray objectAtIndex:1], [secArray objectAtIndex:0],[secArray objectAtIndex:1], [thrdArray objectAtIndex:0],[thrdArray objectAtIndex:1],nil]; 
    NSLog(@"%d", fourthArr.count); 
+0

刪除數組名稱設置一個斷點,你會看到所有你想要的元素在第四個數組 – NullData

相關問題