-NSArray初始化
NSArray *array1=[NSArray arrayWithObjects:@"Shanghai",@"Beijing",@"New York",@"Paris", nil];
NSArray *array2=[[NSArray alloc] initWithObjects:@"Shanghai",@"Beijing",@"New York",@"Paris", nil];
NSArray *array3=@[@"Shanghai",@"Beijing",@"New York",@"Paris"];
输出
array1 count: 4
array2 count: 4
array3 count: 4
-数组元素
NSString *text=@"Panda";//字符串对象
NSInteger number=100;
NSNumber *numberObject1=
[NSNumber numberWithInteger:number ];//使用NSNumber将NSInteger包装为对象
NSNumber *numberObject2=@300u;//使用字面常量初始化
Point point;
point.h=100;
point.v=200;
NSValue *pointObject=
[NSValue value:&point withObjCType:@encode(Point)];//使用NSValue将struct包装为对象
NSNull* nullValue=[NSNull null];
NSArray *array4=@[text, numberObject1,numberObject2,pointObject, nullValue];
NSLog(@"array4: %@" ,array4);
输出
array4: (
Panda,
100,
300,
"<c8006400>",
"<null>"
-数组遍历
//快速枚举
for ( BLNPoint* point in array5)
{
point.x++;
point.y++;
}
//迭代器模式
NSEnumerator *enumerator = [array5 objectEnumerator];
BLNPoint* item;
while (item = [enumerator nextObject])
{
item.x++;
item.y++;
}
//for循环
for (int i=0; i<array5.count; i++) {
NSLog(@"array5[%d],%@",i,array5[i]);
}
-数组排序与查找
//数组查找
BLNPoint* target=[[BLNPoint alloc] initWithX:33 WithY:63];
NSUInteger index1=[array5 indexOfObject:target];
NSUInteger index2=[array5 indexOfObjectIdenticalTo:p3];
NSLog(@"find at %lu", index1);
NSLog(@"find at %lu", index2);
//数组排序
NSArray* sortArray1=[array1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"array1: %@" ,array1);
NSLog(@"sortArray1: %@" ,sortArray1);
-可变数组NSMutableArray
NSMutableArray *muArray1=[NSMutableArray arrayWithObjects:p1,p2,p3,p4, nil];
NSLog(@"before change, muArray1: %@",muArray1);
BLNPoint *p6=[[BLNPoint alloc] initWithX:60 WithY:120];
BLNPoint *p7=[[BLNPoint alloc] initWithX:70 WithY:140];
//修改元素
[muArray1 addObject:p5];
[muArray1 removeObjectAtIndex:2];
[muArray1 insertObject:p6 atIndex:1];
muArray1[0]=p7; //[muArray1 setObject:p7 atIndexedSubscript:0];
NSLog(@"after change, muArray1: %@",muArray1);
//预估容量
int count=100;
NSMutableArray *muArray2=[NSMutableArray arrayWithCapacity:10];
for (int i=0; i< count; i++) {
BLNPoint *pt=[[BLNPoint alloc] initWithX:i*10 WithY:i*20];
[muArray2 addObject: pt];
}