查看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];
}
}
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];
}
}
相关文章推荐
- OBjective-C:在可变数组NSMutableArray中添加相同对象后,进行自定义的排序方式输出
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- NSArray(不可变数组)、NSMutableArray(可变数组)、数组排序、NSNumber(多态:数值和对象互转)、NSValue(将结构体转换成对象)
- 【已解决】iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- 删除可变数组NSMutableArray中的重复数据
- day4_2: 数组NSArray NSMutableArray、值对象NSNumber、NSValue
- OC学习之道:OC对象,字符串和数组的使用方法NSString,NSMutableString ,NSArray,NSMutableArray
- 删除可变数组NSMutableArray中的重复数据
- IOS 根据对象属性值过滤数组中的元素 Filter NSMutableArray with object property
- NSMutableArray数组循环中删除元素不安全的
- iOS OC(四) 字符串NSString 可变数组NSMutableArray 不可变数组NSArray 数值对象类NSNumber NSValue 数值对象类NSNumber
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- 删除可变数组NSMutableArray中的重复数据
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- NSMutableArray和NSArray删除相同元素并只保留一个的方法
- 2015.12.23 OC中的字符串(NSString&NSMutableString) 数组(NSArray&NSMutableArray)
- Objective-C语法-数组NSArray和可变数组NSMutableArray
- 基本结构的数组 NSArry NSMutableArray
- 固定数组NSArray 和 可变NSMutableArray
- mysql插入数据时候先检查表中有没有相同的数据如果有就删除原来记录插入,没有就直接插入