本節學習內容:
1.可變數組的概念
2.可變數組的創建及初始化
3.向可變數組中添加元素
4.數組元素的替換
5.刪除數組中的元素
6.數組的排序
【main.m】
#import <Foundation/Foundation.h>
【1.可變數組的概念】
1.可變數組繼承于不可變數組
2.NSMutableArray,
3.創建可變數組對象,不可變數組中的方法,可變數組對象都可以調用
4.對于可變數組對象,可以直接添加元素,修改元素,刪除元素
5.數組中的元素不能為nil(空對象);
int main(int argc, const char * argv ){
@autoreleasepool{
【2.可變婁組的創建及初始化】
NSMutableArray *array=[[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nul];
NSLog(@"array=%@",array);
打印結果:array=(one,two,three)
1.[構造方法]初始化指定容量大小的可變數組對象(Capacity:20 表示最少可以容易20個數組)
NSMutableArray *array1=[[NSMutableArray alloc] initWithCapacity:20];
2.類方法創建指定容量大小的可變數組對象
NSMutableArray *array2=[NSMutableArray arrayWithCapacity:20] ;
NSLog(@"array1=%@ array2=%@",array1,array2);
打印結果:array1=() array2=()
【3.向可變數組中添加元素】
1.添加一個對象到可變數組中
【array addobject:@"four"];
NSLog(@"array=%@",array);
打印結果:array=(one,two,three,four)
2.把傳數組中的所有元素添加到可變數組中
【array addobjectsFromArray:@“five”,@"six",@"seven"]];
NSLog(@"array=%@",array);
打印結果:array=(one,two,three,four,five,six,seven)
3.在指定位置增加數組元素
【array insertObject:@"hello" atIndex:3]
NSLog(@"array=%@",array);
打印結果:array=(one,two,three,hello,four,five,six,seven)
4.指定下標的多個位置添加數組元素
//創建可變下標集合類對象{1,3,5,6}4個元素
NSMutableIndexSet *mulset=[NSMutableIndexSet indexSetWithIndex:1];
[mulset addIndex:3];
[mulset addIndexsTnRanget:NSMarkeRange(5,2)];
//集合數組對象元素個數要與數組元素個數要相同
【array insertObjects:@[@"baidu",@"hao123",@'google',@"beautify"] atIndex:mulset]
NSLog(@"array=%@",array);
打印結果:array=(one,baidu,two,hao123,three,google,beautify,hello,four,five,six,seven)
5.修改(重置)數組對象
【array setArray:@"hello",@"world",@"baidu",@"hao123"]];
NSLog(@"array=%@",array);
打印結果:array=(hello,world,baidu,hao123)
【4.數組元素的替換】
1.替換指定下標位置的元素
【array replaceObjectAtIndex:2 withObject:@"perfect"];
NSLog(@"array=%@",array);
打印結果:array=(hello,world,perfect,hao123)
2.同時替換下票集合位置的數組元素
NSIndexSet *indexset=[NSIndexSet indexSetWitIndexInRage:NSMakeRange(2,2)]
//集合中的元素就是{2,3}
[array replaceObjectsAtIndexs:(@[@"beautify",@"handsome"]) withobjects:()]
NSLog(@"array=%@",array);
打印結果:array=(hello,world,beautify,handsome);
3.交換數組中的元素
[array exchantObjectAtIndex:1 withObjectAtIndex:3];
NSLog(@"array=%@",array);
打印結果:array=(hello,handsome,beautify,world);
4.替換指定范圍的數組元素
[array replaceObjectsInRange:NSMakRange(1,2)withObjectsFromArray:@[@"one",@"two",@"three",@"four")];
NSLog(@"array=%@",array);
打印結果:array=(hello,one,two,three,four,world);
【5.刪除數組中的元素】
1.刪除指定的數組元素,若當前元素有多個則刪除所有的
[array removeObject:@"one"];
NSLog(@"array=%@",array);
打印結果:array=(hello,two,four,world);
2.刪除指定下票位置的元素
[array removeObjectAiIndex:2]
NSLog(@"array=%@",array);
打印結果:array=(hello,three,four,world);
3.刪除數組中最后一個元素
[array removeLastObjects];
4.刪除數組中所有元素
[array removeAllObjects];
[addObject:@"hello"]
[array insertObject:@"hello" atIndex:2];
NSLog(@"array=%@",array);
打印結果:array=(hello,two,hello,four,world,hello);
5.刪除在指定范圍內出現的數組元素
[array removeObject:@"hello" inRange:NSMakeRange(2,4)];
NSLog(@"array=%@",array);
打印結果:array=(hello,two,four,world,hello);
//刪除所有在傳入數組中出的元素,如果沒有出現就不會刪除,也不會影響
[array removeObjectInArray:@[@"hello",@"world",@"baidu"]];
NSLog(@"array=%@",array);
打印結果:array=(two,four);
【6.數組的排序】
1.排序的數組中的對象必須為同一類型
NSMutableArray *sortArray=[NSMutableArray arrayWitArray:@[@"one",@"two",@"three",,@"four",@"five"]];
//選擇器:傳遞 元素的比較規則
//@selector 創建選擇器的關鍵字,compare:字符比較
//比較規則的方法的返回值大于0時,底層數組元素交換位置
[sortArray sortUsingSelector:(@selector(compare:))];
NSLog(@"sortArray=%@",sortArray);
打印結果:sortArray=(five,four,ome,three,two);
}
//排序不可變數組,需要接收該方法的返回值,可變數組也可以調用
[sortArray sortedArrayUsingSelector:@selector(compare:)]
return 0;
}