您的位置:首页 > 其它

查看NSMutableArray数组中是否相同对象,没有插入,有则删除

2014-03-28 11:07 555 查看
// insert code here...

NSLog(@"数组");

//指定多个字符串创建数组

NSArray *array;

array=[NSArrayarrayWithObjects:@"0-asd",@"1-fds",@"2-哈咯",@"3-个人",nil];

//数组的长度

NSLog(@"数组长度%d",array.count);

//通过索引取得对象

for(int i=0;i<array.count;i++)

{

NSString *secondStr=[array objectAtIndex:i];

NSLog(secondStr,nil);

}

//高速枚举法取得对象,Objective-C2.0开始支持,

for(NSString *str in array)

{

NSLog(str,nil);

}

//对象的追加于删除

//创建空数组

NSMutableArray *MutArray=[NSMutableArray array];

//追加对象

[MutArray addObject:@"A"];

[MutArray addObjectsFromArray:array];

//插入对象

NSString *thstr=@"插入值";

[MutArray insertObject:thstr atIndex:4];

//替换对象

[MutArray replaceObjectAtIndex:2 withObject:@"替换"];

//删除所有对象

//[Mutarray removeAllObjects];

//删除最后的对象

[MutArray removeLastObject];

//删除索引为Index的对象

[MutArray removeObjectAtIndex:0];

//删除所有于object同值的对象

[MutArray removeObject:@"0-asd"];

//删除数组中所有与object等价的对象

[MutArray removeObjectIdenticalTo:thstr];

//删除数组中所有与数组array包含相同的元素

[MutArray removeObjectsInArray:array];

NSLog(@"%@",MutArray);

/*添加对象到数组中,如果数组中已有该字符串,则不添加到数组*/

-(void)TestTheStringhaveOrNotHave:(NSMutableArray*)array string:(NSString*)str

{

// for (int i = 0; i<[antherarray count]; i++)

// {

// NSString * str = [antherarray objectAtIndex:i];

[array addObject:str];

NSIntegerindex= [array indexOfObject:str inRange:NSMakeRange(0, [array count] - 1)];

if(index!= NSNotFound)

{

[array removeObjectAtIndex:index];

}

// }

}

/*删除数组中已经添加相同人名*/

-(void)DeleateTheNotSelectString:(NSMutableArray*)array string:(NSString*)Delstr

{

if([array count]> 0)

{

[array removeObjectIdenticalTo:Delstr];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐