Objective-c之NSArray(NSMutableArray)
1垮媒、創(chuàng)建數(shù)組
NSArray *array = [[NSArray alloc]
initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
2兜蠕、數(shù)組所包含對象個數(shù)
[self.dataArray count]
3社搅、獲取指定索引處的對象
[self.dataArray objectAtIndex:2]
4、從一個數(shù)組拷貝數(shù)據(jù)到另一數(shù)組(可變數(shù)級)
//arrayWithArray:
//NSArray*array1 = [[NSArray alloc] init];
NSMutableArray*MutableArray = [[NSMutableArray alloc] init];
NSArray*array = [NSArray arrayWithObjects:
@"a",@"b",@"c",nil];
NSLog(@"array:%@",array);
MutableArray= [NSMutableArray arrayWithArray:array];
NSLog(@"MutableArray:%@",MutableArray);
array1= [NSArray arrayWithArray:array];
NSLog(@"array1:%@",array1);
5度迂、COPY
NSMutableArray*newArray = [[NSMutableArray alloc] init];
NSArray*oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(inti = 0; i < [oldArray count]; i++)
{
obj= [[oldArray objectAtIndex:i] copy];
[newArrayaddObject: obj];
}
//
NSLog(@"newArray:%@",newArray);
[newArrayrelease];
6迅矛、深COPY
//NSMutableArray *newArray = [[NSMutableArrayalloc] init];
NSArray*oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
newArray=(NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault,(CFPropertyListRef)oldArray,kCFPropertyListMutableContainers);
NSLog(@"newArray:%@",newArray);
[newArrayrelease];
7臭挽、快速枚舉
//NSMutableArray *newArray = [[NSMutableArray alloc]init];
NSArray*oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(idobj in oldArray)
{
[newArrayaddObject: obj];
}
//
NSLog(@"newArray:%@",newArray);
[newArrayrelease];
8、切分數(shù)組
NSString *string = [[NSString alloc]initWithString:@"One,Two,Three,Four"];
NSLog(@"string:%@",string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[string release];
9、從數(shù)組合并元素到字符串
NSArray *array = [[NSArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString*string = [array componentsJoinedByString:@","];
NSLog(@"string:%@",string);
10趴酣、 給數(shù)組分配容量
array = [NSMutableArrayarrayWithCapacity:20];
11梨树、 在數(shù)組末尾添加對象
NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"One",@"Two",@"Three",nil];
[array addObject:@"Four"];
NSLog(@"array:%@",array);
12、刪除數(shù)組中指定索引處對象
NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"One",@"Two",@"Three",nil];
[array removeObjectAtIndex:1];
NSLog(@"array:%@",array);