//創(chuàng)建一個數(shù)組
NSArray*myArray;
NSValue*aValue = [NSNumbernumberWithInt:5];
NSString*aString =@"nihoa";
// NSString *aString = [NSString stringWithFormat:@"nihao"];
myArray = [NSArrayarrayWithObjects:aValue, aString,nil];//用便利構(gòu)造器
NSLog(@"%@",myArray);
NSArray*arr = [[NSArrayalloc]initWithObjects:@"one",@"two",nil];//用alloc+init
NSLog(@"%@",arr);
//查詢數(shù)組中是否有某個元素返回值是BOOL類型
if([arrcontainsObject:@"one"])
NSLog(@"YES");
else
NSLog(@"NO");
//元素個數(shù)
NSLog(@"%lu",[arrcount]);
[arrrelease];
//檢索元素 并存放在C語言的數(shù)組中
NSArray*arr1 = [[NSArrayalloc]initWithObjects:@"one",@"two",@"three",@"four",nil];
id*objects;
NSUIntegercount = [arr1count];
objects =malloc(sizeof(id) * count);//objects數(shù)組名
[arr1getObjects:objects];
for(NSUIntegeri =0; i < count; i++) {
NSLog(@"object at index %lu: %@", i, objects[i]);
}
free(objects);
//檢索某個下標的元素
NSLog(@"%@",[arr1objectAtIndex:0]);
//檢索某個元素的下標
NSLog(@"%lu",[arr1indexOfObject:@"two"]);
NSLog(@"%lu",[arr1indexOfObject:@"two"inRange:NSMakeRange(0,3)]);
//在數(shù)組中增加元素
NSArray*arr2 = [arr1arrayByAddingObject:@"tiandadida"];
NSLog(@"%@",arr2);
[arr1release];
//排序
NSArray*arrnumber= [[NSArrayalloc]initWithObjects:@"nihao",@"wo",@"helloword",@"zhonggu",nil];
NSArray*te = [arrnumbersortedArrayUsingComparator: ^(NSString*s,NSString*s2){
if(s.length< s2.length){
returnNSOrderedDescending;
}
if(s.length> s2.length){
returnNSOrderedAscending;
}
// NSLog(@"...........................");
returnNSOrderedSame;
}];
NSLog(@"te=%@.",te);
//firstObjectCommonWithArray的用法
NSArray*arrnum= [[NSArrayalloc]initWithObjects:@"haoma",@"wo",@"helloword",@"zhongguo",nil];
NSLog(@"%@",[arrnumberfirstObjectCommonWithArray:arrnum]);
//快速枚舉
NSArray*ar = [NSArrayarrayWithObjects:@"one",@"two",@"three",nil];
for(NSString*elementinar) {
NSLog(@"element: %@",element);
}
//lastobject
NSLog(@"%@",[arlastObject]);
//把數(shù)組元素(字符串),連接起來.
NSString*string = [arcomponentsJoinedByString:@"->"];
NSLog(@"%@",string);
//某個范圍的子串
NSRangetheRange;
theRange.location=0;//range的起點
theRange.length= [arcount] /2;//range的長度
NSArray*halfArray = [arsubarrayWithRange:theRange];
NSLog(@"%@",halfArray);
//description Returns a string that represents the contents of the array, formatted as a property list.
NSString*test = [ardescription];
NSLog(@"%@",test);
}