NSArray * arr1 = @[@"1 2 3",@"345",[NSNull null],@"789"];
NSArray * arr3 = [[NSArray alloc]initWithArray:arr1];
NSLog(@"arr3 == %@",arr3);
//将c数组转换为oc数组对象
//oc不允许将nil插入到数组中,因为nil作用是数组的结束标记
NSString * carr[10] = {@"123",@"456",@"789"};
NSArray * arr4 = [[NSArray alloc]initWithObjects:carr count:2];
NSLog(@"arr4 == %@",arr4);
//获取元素个数
NSUInteger count = [arr4 count];
NSLog(@"count = %ld",count);
//获取指定下标元素
NSLog(@"arr3.1 == %@",[arr3 objectAtIndex:1]);
//获取第一个元素或者最后一个元素
id arrf = [arr3 firstObject];
id arrl = [arr3 lastObject];
NSLog(@"%@ %@",arrf,arrl);
//获取子集
NSArray *arr2 = @[@"111", @"222", @"333", @"444", @"555", @"666"];
//取出连续范围的元素作为子集内容
NSArray *sub1 = [arr2 subarrayWithRange:NSMakeRange(1, 3)];
NSLog(@"sub1 = %@", sub1);
//获取任意元素作为子集
//1.列出所需要元素的下标清单
NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
//1-1.将要获取元素的下标写入清单
for (NSInteger i = 0; i < [arr2 count]; i++)
{
if (i % 2 != 1)
{
[indexs addIndex:i];
}
}
//2.将清单交给数组
//id obj =[arr2 objectAtIndex:0]; //获取单个元素,返回单个元素
NSArray *sub2 = [arr2 objectsAtIndexes:indexs];//获取下标清单里的所有元素,返回一个元素集合(数组)
NSLog(@"sub2 = %@", sub2);
//获取最长字符串和最小字符串
//快速枚举
NSArray *arr5 = @[@"123456789", @"22222", @"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 *muarr1 = [[NSMutableArray alloc] initWithCapacity:30];
NSMutableArray *muarr2 = [NSMutableArray arrayWithCapacity:30];
//添加元素
[muarr1 addObject:@"123"];
[muarr1 addObject:@"456"];
[muarr2 addObjectsFromArray:muarr1];
[muarr2 addObject:@"789"];
[muarr2 addObject:@"45"];
[muarr2 addObject:@"789"];
[muarr2 addObject:@"789"];
NSLog(@"muarr1 = %@",muarr1);
NSLog(@"muarr2 = %@",muarr2);
//插入元素
//插入下标的范围: 0<=index<=count
[muarr1 insertObject:@"aaa" atIndex:1];
[muarr1 insertObject:@"bbb" atIndex:[muarr1 count]];
NSLog(@"muarr1 = %@",muarr1);
NSLog(@"删除前的muarr2 == %@",muarr2);
//[muarr2 removeAllObjects];//删除所有元素
//[muarr2 removeLastObject];//删除最后一个元素
//[muarr2 removeObject:@"789"];//删除指定对象,会将所有对象删除
//[muarr2 removeObject:@"789" inRange:NSMakeRange(1, 2)];//删除指定范围中的指定对象
//[muarr2 removeObjectAtIndex:0];//删除指定下标的元素
//[muarr2 removeObjectsInRange:NSMakeRange(2, 2)];//删除指定范围中得元素
//[muarr2 removeObjectIdenticalTo:@"789"];
//[muarr2 removeObjectIdenticalTo:@"789" inRange:NSMakeRange(2, 1)];
//如果要删除不连续的元素,则先把要删除的元素放在一个清单中,然后再删除该清单
//比如要删下标为偶数的
NSMutableIndexSet *indexs2 = [NSMutableIndexSet indexSet];
for (int i = 0; i < [muarr2 count]; i++) {
if (i % 2 == 0) {
[indexs2 addIndex:i];
}
}
//删除清单中得元素
// [muarr2 removeObjectsAtIndexes:indexs2];
//替换指定下标的值
//[muarr2 replaceObjectAtIndex:2 withObject:@"aaa"];
//NSArray *arr = @[@"qqqq",@"www",@"ee"];//注意数组越界
//[muarr2 replaceObjectsAtIndexes:indexs2 withObjects:arr];
//[muarr2 replaceObjectsInRange:NSMakeRange(1, 3) withObjectsFromArray:arr];// qq, qq, ww, ee, ee,在指定范围内,修改数组中得内容。
//交换数组中得内容
// [muarr2 exchangeObjectAtIndex:1 withObjectAtIndex:3];
//用oc对c语言中得数组进行排序
for (int i = 0; i < [muarr2 count]; ++i) {
for (int j = i + 1; j < [muarr2 count]; ++j) {
if ([muarr2[i] length] < [muarr2[j] length]) {
[muarr2 exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"删除后的muarr2 == %@",muarr2);
排序
NSArray * arr1 = @[@"1",@"3",@"2",@"9",@"5",@"4"];
NSMutableArray * arr = [[NSMutableArray alloc]initWithArray:arr1];
for (int i = 0; i<[arr count]; ++i) {
for (int j = i + 1; j<[arr count]; ++j) {
if ([arr[i] floatValue] > [arr[j] floatValue]) {
[arr exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
// NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
// NSInteger num = [arr count]/2;
// [indexs addIndex:num];
// NSArray * sub = [arr objectsAtIndexes:indexs];
// NSLog(@"%@",sub[0]);
网友评论