标签:
//创建数组对象,NSArray不可变数组
//[NSNull null]空对象
//NSArray *arr = [[NSArray alloc] initWithObjects:@"234",@"543",@"asb",nil];
NSArray *arr1 = @[@"1 2 3",@"234",[NSNull null],@"789"];
NSArray *arr3 = [[NSArray alloc] initWithArray:arr1];
NSLog(@"%@",arr3);
//将c数组转化为oc数组对象
NSString *carr[10] = {@"123",@"456",@"789"};
NSArray *arr4 = [[NSArray alloc] initWithObjects:carr count:3];
NSLog(@"%@",arr4);
//获取数组元素个数
NSUInteger count = [arr4 count]; //NSUInteger无符号的 NSInteger有符号
NSLog(@"count is:%ld" , count);
//获取指定下标元素
NSLog(@"%@",[arr3 objectAtIndex:1]);
//获取第一个元素或者最后一个元素
NSLog(@"%@",[arr3 firstObject]);
NSLog(@"%@",[arr3 lastObject]);
//获取arr2的子集
NSArray *arr2 = @[@"111",@"222",@"333",@"444",@"555",@"666"];
//取出arr2中的连续范围的元素作为子集内容
NSArray *sub1 = [arr2 subarrayWithRange:NSMakeRange(1, 3)]; //sub1为新的子集
NSLog(@"%@",sub1);
//获取任意元素作为子集
//1、列出做需要元素的下标清单
// NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
//1-1将要获取元素的下标写入清单
//for (NSInteger i = 0; i<[arr2 count]; i++) {
// if (i % 2 != 0)
// {
// [indexs addIndex:i];
// }
//}
//2、将清单交给数组
// NSArray *sub2 = [arr2 objectAtIndex:indexs];//获取下标清单里的所有元素,返回一个元素集合数组
// NSLog(@"sub2 = %@",sub2);
//获取最长字符串和最小字符串
//快速枚举
NSArray *arr5 = @[@"123456789", @"222", @"333", @"errr", @"444", @"5", @"666"];
NSInteger maxLength = [arr5[0] length];
NSInteger minLength = [arr5[0] length];
NSString *maxString = nil;
NSString *minString = nil;
for (id obj in arr5) {
if (maxLength <= [obj length]) {
maxLength = [obj length];
maxString = obj;
}
if (minLength >= [obj length]) {
minLength = [obj length];
minString = obj;
}
}
NSLog(@"maxString = %@, minString = %@",maxString,minString);
////////////////////////////////////////////////////////////////////
//=========可变数组===========可变数组============可变数组===========//
////////////////////////////////////////////////////////////////////
//创建可变数组对象
NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:@"232",@"aaa",@"342",@"987",@"876",nil];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:@"dada",@"zas",@"4rfc",@"7uj",nil];
NSLog(@"array1 = %@ array2 = %@",array1,array2);
//插入元素
//插入下标的范围0 <= index <= count
[array1 insertObject:@"JXF" atIndex:1];
[array1 insertObject:@"WG" atIndex:[array1 count]];//这样是把“WG”插入到最后
[array1 insertObject:@"GG" atIndex:3];//这样写是把“GG”插入到下标为3的位置!
NSLog(@"array1 = %@",array1);
//删除元素
//[array2 removeAllObjects];//删除所有
// [array2 removeLastObject];//删除最后一个
//[array2 removeObject:@"zas"];
// NSLog(@"%@",array2);
// [array2 removeObject:@"zas" inRange:NSMakeRange(1, 2)];
// NSLog(@"%@",array2);
// [array2 removeObjectAtIndex:2];//删除指定下标的元素
// [array2 removeObjectIdenticalTo:@"zas"];
// [array2 removeObjectIdenticalTo:@"zas" inRange:NSMakeRange(1, 2)];
//如果要删除不连续的元素,则先把要删除的元素放在一个清单中,然后再删除该清单
//比如要删下标为偶数的
NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
for (int i = 0; i < [array2 count]; i++) {
if (i % 2 == 0) {
[indexs addIndex:i];
}
}
//删除清单中得元素
[array2 removeObjectsAtIndexes:indexs];
NSLog(@"%@",array2);
//修改数组中的内容
[array2 replaceObjectAtIndex:1 withObject:@"NMB"];
NSLog(@"%@",array2);
NSArray *arr = @[@"000",@"111",@"aaa"];
[array2 replaceObjectsAtIndexes:indexs withObjects:arr];
NSLog(@"%@",array2);
//交换数组中的的内容
[array2 exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"%@",array2);
//用OC对C语言中的数组排序
for (int i = 0; i<[array2 count]; i++) {
for (int j =i+1 ;j<[array2 count];j++) {
if ([array2[i] length ]>[array2[j] length]) {
[array2 exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"array2 = %@",array2);
标签:
原文地址:http://www.cnblogs.com/54pangzi/p/4471777.html