NSArray * arr1 = @[@"1 2 3",@"345",[NSNull null],@"789"];
NSArray * arr3 = [[NSArray alloc]initWithArray:arr1];
NSLog(@"arr3 == %@",arr3);
//將c數(shù)組轉(zhuǎn)換為oc數(shù)組對(duì)象
//oc不允許將nil插入到數(shù)組中,因?yàn)閚il作用是數(shù)組的結(jié)束標(biāo)記
NSString * carr[10] = {@"123",@"456",@"789"};
NSArray * arr4 = [[NSArray alloc]initWithObjects:carr count:2];
NSLog(@"arr4 == %@",arr4);
//獲取元素個(gè)數(shù)
NSUInteger count = [arr4 count];
NSLog(@"count = %ld",count);
//獲取指定下標(biāo)元素
NSLog(@"arr3.1 == %@",[arr3 objectAtIndex:1]);
//獲取第一個(gè)元素或者最后一個(gè)元素
id arrf = [arr3 firstObject];
id arrl = [arr3 lastObject];
NSLog(@"%@ %@",arrf,arrl);
//獲取子集
NSArray *arr2 = @[@"111", @"222", @"333", @"444", @"555", @"666"];
//取出連續(xù)范圍的元素作為子集內(nèi)容
NSArray *sub1 = [arr2 subarrayWithRange:NSMakeRange(1, 3)];
NSLog(@"sub1 = %@", sub1);
//獲取任意元素作為子集
//1.列出所需要元素的下標(biāo)清單
NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
//1-1.將要獲取元素的下標(biāo)寫入清單
for (NSInteger i = 0; i < [arr2 count]; i++)
{
if (i % 2 != 1)
{
[indexs addIndex:i];
}
}
//2.將清單交給數(shù)組
//id obj =[arr2 objectAtIndex:0]; //獲取單個(gè)元素,返回單個(gè)元素
NSArray *sub2 = [arr2 objectsAtIndexes:indexs];//獲取下標(biāo)清單里的所有元素,返回一個(gè)元素集合(數(shù)組)
NSLog(@"sub2 = %@", sub2);
//獲取最長(zhǎng)字符串和最小字符串
//快速枚舉
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);
//創(chuàng)建可變數(shù)組對(duì)象
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);
//插入元素
//插入下標(biāo)的范圍: 0<=index<=count
[muarr1 insertObject:@"aaa" atIndex:1];
[muarr1 insertObject:@"bbb" atIndex:[muarr1 count]];
NSLog(@"muarr1 = %@",muarr1);
NSLog(@"刪除前的muarr2 == %@",muarr2);
//[muarr2 removeAllObjects];//刪除所有元素
//[muarr2 removeLastObject];//刪除最后一個(gè)元素
//[muarr2 removeObject:@"789"];//刪除指定對(duì)象,會(huì)將所有對(duì)象刪除
//[muarr2 removeObject:@"789" inRange:NSMakeRange(1, 2)];//刪除指定范圍中的指定對(duì)象
//[muarr2 removeObjectAtIndex:0];//刪除指定下標(biāo)的元素
//[muarr2 removeObjectsInRange:NSMakeRange(2, 2)];//刪除指定范圍中得元素
//[muarr2 removeObjectIdenticalTo:@"789"];
//[muarr2 removeObjectIdenticalTo:@"789" inRange:NSMakeRange(2, 1)];
//如果要?jiǎng)h除不連續(xù)的元素,則先把要?jiǎng)h除的元素放在一個(gè)清單中汤功,然后再刪除該清單
//比如要?jiǎng)h下標(biāo)為偶數(shù)的
NSMutableIndexSet *indexs2 = [NSMutableIndexSet indexSet];
for (int i = 0; i < [muarr2 count]; i++) {
if (i % 2 == 0) {
[indexs2 addIndex:i];
}
}
//刪除清單中得元素
// [muarr2 removeObjectsAtIndexes:indexs2];
//替換指定下標(biāo)的值
//[muarr2 replaceObjectAtIndex:2 withObject:@"aaa"];
//NSArray *arr = @[@"qqqq",@"www",@"ee"];//注意數(shù)組越界
//[muarr2 replaceObjectsAtIndexes:indexs2 withObjects:arr];
//[muarr2 replaceObjectsInRange:NSMakeRange(1, 3) withObjectsFromArray:arr];// qq, qq, ww, ee, ee,在指定范圍內(nèi)黎侈,修改數(shù)組中得內(nèi)容荤懂。
//交換數(shù)組中得內(nèi)容
// [muarr2 exchangeObjectAtIndex:1 withObjectAtIndex:3];
//用oc對(duì)c語言中得數(shù)組進(jìn)行排序
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]);