1

在 NSMutableArray 我有这样的数据,

    MutableArray1:(
           "22.298166 , 73.165809",
           "22.300598 , 73.167183",
           "22.298101 , 73.166188",
           "22.298128 , 73.166194"
           "22.298130 , 73.166194"
          )

我想将 MutableArray1 中的最后三条数据复制到 MutableArray2 中。

请建议我该怎么做?

4

8 回答 8

8

你可以这样使用subarrayWithRange:NSArray

NSRange range = NSMakeRange([mutableArray1 count] - 3, 3);
NSMutableArray *mutableArray2 = [NSMutableArray arrayWithArray:
                                  [mutableArray1 subarrayWithRange:range]];
于 2012-12-04T13:23:03.923 回答
4

它非常简单

应用while循环

int i=1;
while (i<4 && i < [array1 count])
{
    [array2 addObject:[array1 objectAtIndex:([array1 count]-i)]];
    i++;
}

希望对你有帮助

于 2012-12-04T13:23:26.743 回答
4

此代码将帮助您

NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil];

    NSMutableArray * otherArray = [[NSMutableArray alloc]init];
    for (int i= array.count-3; i<array.count; i++) {
        [otherArray addObject:[array objectAtIndex:i]];
    }
    NSLog(@"%@",otherArray);
于 2012-12-04T13:26:00.747 回答
1
for (int i=[array1 count] - 4 ; i<[array1 count] ; i++){
    [array2 addObject:[array1 objectAtIndex:i]];
}
于 2012-12-04T13:21:56.413 回答
0

为此,您可以实现自己的自定义代码。

if([yourArray count] > 3)
{
  for (int loop = [yourArray count]; loop >= ([yourArray count]-3) ; loop--)
     [secondArray addObject:[yourArray objectAtIndex:loop-1]];
}
于 2012-12-04T13:24:15.647 回答
0
use this
   MutableArray *array = [main_array subarrayWithRange:NSMakeRange(main_array.count-3, main_array.count)];
于 2012-12-04T13:24:58.583 回答
0
[MutableArray2 addObject:[MutableArray1 objectAtIndex:[MutableArray1 count]-1]] ;
[MutableArray2 addObject:[MutableArray1 objectAtIndex:[MutableArray1 count]-2]] ;
[MutableArray2 addObject:[MutableArray1 objectAtIndex:[MutableArray1 count]-3]] ;
于 2012-12-04T13:26:56.437 回答
0
    if([yourArray1 count] > 3){

        for (int i=[yourArray1 count] - 4 ; i<[yourArray1 count] ; i++){
          [yourArray2 addObject:[yourArray1 objectAtIndex:i]];
        }
    }
    else{
        for (int i= 0 ; i<[yourArray1 count] ; i++){
          [yourArray2 addObject:[yourArray1 objectAtIndex:i]];
        }
    }
于 2012-12-04T13:29:26.653 回答