如何在单个可变数组中组合两个可变数组值?

时间:2013-05-22 06:01:11

标签: iphone ios objective-c

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{


    NSError *error;
    json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"json.... %@",json);


    id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];

    NSLog(@"jsonObject=%@", jsonObject);

    NSDictionary *checkArray=[json valueForKey:@"ND"];

    NSArray *tel = [checkArray valueForKey:@"FN"];


    testArray = [[NSMutableArray alloc]init];
    testArray1 = [[NSMutableArray alloc]init];

    newsarray = [[NSMutableArray alloc]init];


    for (id photo in tel)
    {
        if (photo == [NSNull null])
        {
            NSString *test8;
            test8 = @"empty";
            [testArray addObject:test8];

        }
        else
        {
            // photo isn't null. It's an array
            NSArray *innerPhotos = photo;

            [testArray addObject:photo];

        }

    }


    NSArray *tel1 = [checkArray valueForKey:@"LN"];

    for (id photo1 in tel1)
    {
        if (photo1 == [NSNull null])
        {
            NSString *test8;
            test8 = @"empty";
            [testArray1 addObject:test8];

        }
        else
        {
            // photo isn't null. It's an array
            //NSArray *innerPhotos1 = photo1;

            [testArray1 addObject:photo1];

        }

    }


    newsarray = [NSMutableArray arrayWithArray:[testArray arrayByAddingObjectsFromArray:testArray1]];
    NSLog(@"testArray =%@",newsarray);

这里我想组合两个数组值“testArray”和“testArray1”

我的mutablearray值是

testArray = aa,bb,cc,dd ...... testArray1 = xx,yy,zz,ss ...

我希望我的输出像

aa xx,bb yy,cc zz,dd ss

7 个答案:

答案 0 :(得分:1)

你可以做类似下面的事情。

 NSMutableArray *aryFinal=[[NSMutableArray alloc]init];

int count = [testArray count]+[testArray1 count];
for(int i=0;i<count;i++)
{
 if(i%2==0)
    [aryFinal addobject:[testArray objectAtIndex:i]];
 else
    [aryFinal addobject:[testArray1 objectAtIndex:i]];

 }

让我知道它的工作与否!!!

答案 1 :(得分:1)

试试这个:

for (int i=0;i<[testArray count];i++){
    NSString *tmpObject=[NSString stringWithFormat:@"%@ %@",
                            [testArray objectAtIndex:i],  
                            [testArray1 objectAtIndex:i]];

    [newArray addObject tmpObject];
    tmpObject=nil;
}

答案 2 :(得分:1)

 NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"AA",@"BB",@"CC" nil];
 NSArray *array2 = [NSArray arrayWithObjects:@"XX",@"YY",@"ZZ" nil];

    for (int i=0; i<[array1 count];i++)
        [array1 replaceObjectAtIndex:i 
                withObject:[NSString stringWithFormat:@"%@ %@",
                            array1[i],
                            array2[i]]];
    NSLog(@"%@",array1);

<强>输出:

  

“AA XX”,“BB YY”,“CC ZZ”

答案 3 :(得分:0)

简单地添加两个数组:

[testArray setArray: testArray1];

但如果你想得到理想的结果:

NSMutableArray *arrFinal=[[NSMutableArray alloc]init];

for(int i = 0; i < (testArray.count + testArray1.count); i++)
{
 if(i%2 == 0)
    [arrFinal addobject:[testArray objectAtIndex:i]];
 else
    [arrFinal addobject:[testArray1 objectAtIndex:i]];
}

答案 4 :(得分:0)

表示aa xx,bb yy,cc zz,dd ss输出:

int i=-1;
for(int k =0;k<[testArray1 count];++k)
{
    i=i+2;
    [testArray insertObject:[testArray1 objectAtIndex:k] atIndex:i];
}

答案 5 :(得分:0)

不需要去第三阵列。您可以使用replaceObjectAtIndex的{​​{1}}方法。

这样..

NSMutableArray

答案 6 :(得分:0)

NSMutableArray *array1,*array2;
    //////array1 and array2 initialise it with your values
    NSMutableArray *finalArray = [[NSMutableArray alloc]init]

    int totalcount = 0;

    if (array1.count > array2.count) {
       totalcount =  array1.count;
    }
    else
        totalcount =  array2.count;

    for (int i = 0; i<totalcount; i++) {
        if (i <= array1.count-1) {
            [finalArray addObject:[array1 objectAtIndex:i]];
        }
        if (i <= array2.count-1) {
            [finalArray addObject:[array2 objectAtIndex:i]];
        }
    }