如何在单个可变数组中组合两个可变数组值? [重复]

时间:2021-06-26 12:20:28

This question already has an answer here:

这个问题在这里已有答案:

- (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);

here i want to combine two array values "testArray" and "testArray1"

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

my mutablearray values are

我的可变阵列值是

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

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

i would like to expect my output like

我希望我的输出像

aa xx, bb yy, cc zz, dd ss

aa xx,bb yy,cc zz,dd ss

7 个解决方案

#1


1  

Try this:

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  

you can do something like below..

你可以做下面的事情..

 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]];

 }

let me know it is working or not!!!

让我知道它的工作与否!

#3


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);

Output:

"AA XX","BB YY","CC ZZ"

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

#4


0  

To simply add two arrays:

要简单地添加两个数组:

[testArray setArray: testArray1];

But if you want desired result:

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

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]];
}

#5


0  

Do not need to go for third array. You can use replaceObjectAtIndex method of NSMutableArray.

不需要去第三阵列。您可以使用NSMutableArray的replaceObjectAtIndex方法。

This way..

    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"aa",@"bb", nil];
    NSArray *array2 = [NSArray arrayWithObjects:@"xx",@"yy", nil];

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

#6


0  

for aa xx, bb yy, cc zz, dd ss output:

对于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];
}

#7


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]];
        }
    }

#1


1  

Try this:

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  

you can do something like below..

你可以做下面的事情..

 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]];

 }

let me know it is working or not!!!

让我知道它的工作与否!

#3


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);

Output:

"AA XX","BB YY","CC ZZ"

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

#4


0  

To simply add two arrays:

要简单地添加两个数组:

[testArray setArray: testArray1];

But if you want desired result:

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

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]];
}

#5


0  

Do not need to go for third array. You can use replaceObjectAtIndex method of NSMutableArray.

不需要去第三阵列。您可以使用NSMutableArray的replaceObjectAtIndex方法。

This way..

    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"aa",@"bb", nil];
    NSArray *array2 = [NSArray arrayWithObjects:@"xx",@"yy", nil];

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

#6


0  

for aa xx, bb yy, cc zz, dd ss output:

对于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];
}

#7


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]];
        }
    }