Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2618374
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-08-06 14:13:10

http://blog.csdn.net/ztp800201/article/details/7777715

一、NSArray是静态数组,创建后数组内容及长度不能再修改。

实例:


  1. //用arrayWithObjects初始化一个不可变的数组对象。  
  2. //初始化的值之间使用逗号分开,以nil结束。  
  3. NSArray6 *city = [NSArray arrayWithObjects:@"上海",@"广州",@"重庆",nil];  
  4.   
  5. for(int i=0; i < [city count];i++){  
  6.     NSLog(@"%@",[city objectAtIndex:i]);  
  7. }  

运行结果:


上海
广州
重庆


NSArray常用方法:


  1. +(id)arrayWithObjects:obj1,obj2,...nil             //创建一个新的数组,obj1,obj2,.., 以nil结束。  
  2. -(BOOL)containsObject:obj                          //确定数组中是否包含对象obj  
  3. -(NSUInterger)count                                //数组中元素的个数  
  4. -(NSUInterger)indexOfObject:obj                    //第一个包含obj元素的索引号  
  5. -(id)ObjectAtIndex:i                               //存储在位置i的对象  
  6. -(void)makeObjectsPerformSelector:(SEL)selector    //将selector提示的消息发送给数组中的每一个元素  
  7. -(NSArray*)sortedArrayUsingSelector:(SEL)selector  //根据selector提定的比较方法对数组进行排序  
  8. -(BOOL)writeToFile:path atomically:(BOOL)flag      //将数组写入指定的文件中。如果flag为YES,则需要先创建一个临时文件  

二、NSMutableArray是动态数组,可以动态增加数组中的元素,同样NSMutableArray是NSArray的子类。
实例:



  1. //用arrayWithCapacity创建一个长度为5的动态数组  
  2. NSMutableArray *nsma = [MSMutableArray arrayWithCapacity:5];  
  3. for(int i=0;i<=50;i++) {  
  4.    if( i%3 == 0 ) {  
  5.        [nsma addObject:[NSNumber numberWithInteger:i]];   //用addObject给数组nsma增加一个对象  
  6.     }  
  7. }  
  8.   
  9. //输出数组中的元素  
  10. for(int i=0;i<[nsma count];i++) {  
  11.    NSLog(@"%li",(long)[[nsma objectAtIndex] integerValue]);  
  12. }  

数组排序例子:


student.h


  1. #import <Foundation/Foundation.h>  
  2. @interface Student: NSObject {  
  3.   NSString: *name;  
  4.   int age;  
  5. }  
  6. @property (copy,nonatomic) NSString* name;  
  7. @property int age;  
  8. -(void)print;  
  9. -(NSComparisonResult)compareName:(id)element;  
  10.   
  11. @end  

StudentTest.m


  1. #import <Foundation/Foundation.h>  
  2. #import "student.h"  
  3.   
  4. int main( int argc, const char* agrv[]) {  
  5.   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  6.   Student* stu1 = [[Student alloc] init];  
  7.   Student* stu2 = [[Student alloc] init];  
  8.   Student* stu3 = [[Student alloc] init];  
  9.    
  10.   [stu1 setName:@"Sam"];  
  11.   [stu1 setAge:30];  
  12.   [stu2 setName:@"Lee"];  
  13.   [stu2 setAge:23];  
  14.   [stu3 setName:@"Alex"];  
  15.   [stu3 setAge:26];  
  16.   
  17.   NSMutableArray *students = [[NSMutableArray alloc] init];  
  18.   [students addObject:stu1];  
  19.   [students addObject:stu2];  
  20.   [students addObject:stu3];  
  21.    
  22.   NSLog(@"排序前:");  
  23.   for(int i=0; i<[students count];i++) {  
  24.     Student *stu4 = [students objectAtIndex:i];  
  25.     NSLog(@"Name:%@,Age:%i",[stu4 name],[stu4 age]);  
  26.   }  
  27.   
  28.   [students sortUsingSelector:@selector(compareName:)];  
  29.   
  30.   NSLog(@"排序后:");  
  31.   for( Student *stu4 in students ) {  
  32.     NSLog(@"Name:%@,Age:%i", stu4.name, stu4.age);  
  33.   }  
  34.     
  35.   [students release];  
  36.   [stu1 release];  
  37.   [sut2 release];  
  38.   [stu3 release];  
  39.   [pool drain];  
  40.   
  41.    return 0;  
  42. }  

结果:



[plain] view plaincopy
  1. 排序前:  
  2. Name:Sam,Age:30  
  3. Name:Lee,Age:23  
  4. Name:Alex,Age:26  
  5. 排序后:  
  6. Name:Alex,Age:26  
  7. Name:Lee,Age:23  
  8. Name:Sam,Age:30  

NSMutableArray常用方法:



  1. +(id)array                                    //创建一个空数组  
  2. +(id)arrayWithCapacity:size                   //创建一个容量为size的数组  
  3. -(id)initWithCapacity:size                    //初始化一个新分配的数,指定容量为size  
  4. -(void)addObject:obj                          //将obj增加到数组中  
  5. -(void)insertObject:obj atIndex:i             //将obj插入数据的i元素  
  6. -(void)replaceObjectAtIndex:i withObject:obj  //用obj替换第i个索引的对象  
  7. -(void)removeObject:obj                       //从数组中删除所有是obj的对象  
  8. -(void)removeObjectAtIndex:i                  //从数组中删除索引为i的对象  
  9. -(void)sortUsingSelector:(SEL)selector        //用selector指示的比较方法进行排序  

阅读(481) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~