首先我们必须知道数组的概念:数组是有序的对象集合,一般情况下,一个数组的对象都是相同类型的。数组当中也存在可变数组和不可变数组。

  1. 不可变数组 (NSArray)

  可变数组 NSMutable 是不可变数组的子类,下面我们先了解一下 NSArray 的用法:

    

  下面通过一个程序例子,来看下它们的具体用法:

  1)首先是创建一个数组常用的3种方法:

   

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end int main(int argc,char **argv){
@autoreleasepool {
// NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu", nil]; 普通的方法 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; // 常用的简便的方法
for(NSString *temp in array1)
NSLog(@"temp = %@",temp); NSLog(@"-----------------");
// NSArray *array2 = [NSArray arrayWithObjects:@11,@22,@33, nil];
NSArray *array2 = @[@,@,@];
for(NSNumber *temp in array2)
NSLog(@"temp = %li",[temp integerValue]); NSLog(@"-----------------");
Student *stu1 = [[Student alloc] init:@"zhgnsan" andage:];
Student *stu2 = [[Student alloc] init:@"lisi" andage:];
Student *stu3 = [[Student alloc] init:@"wangwu" andage:]; // NSArray *array3 = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];
NSArray *array3 = @[stu1,stu2,stu3];
for(Student *temp in array3)
NSLog(@"name = %@,age = %i",[temp _name],[temp _age]); }
}

  2)遍历数组的方法:

  

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; //通过新增for遍历
for(NSString *temp in array1)
NSLog(@"temp = %@",temp); NSLog(@"--------------");
NSUInteger count = [array1 count];
for(NSUInteger i = ; i < count; i++)
NSLog(@"array1[%li] = %@",i,array1[i]); NSLog(@"--------------");
for(NSUInteger i = ; i < count; i++)
NSLog(@"array1[%li] = %@",i,[array1 objectAtIndex:i]); NSLog(@"--------------");
[array1 makeObjectsPerformSelector:@selector(print)];//使array中每个成员都执行一次print函数 NSLog(@"------block--------"); //通过函数遍历
[array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"array1[%li] = %@",idx,obj);
if(idx == )
*stop = YES;
}]; NSLog(@"---------------");
NSEnumerator *enumer = [array1 objectEnumerator]; //通过枚举器进行遍历
NSString *temp = nil;
while(temp = [enumer nextObject]){
NSLog(@"temp = %@",temp);
} NSLog(@"---------------");
NSLog(@"array1 = %@",array1);
NSLog(@"---------------");
NSLog(@"array1 = %@",[array1 description]);
}
}

  3)写入和从文件读取的方法

  

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; //通过新增for遍历
for(NSString *temp in array1)
NSLog(@"temp = %@",temp); NSString *filename = @"name.plist";
BOOL iswrite = [array1 writeToFile:filename atomically:YES]; //写入文件
if(iswrite)
NSLog(@"write to file ok");
else
NSLog(@"write to file error"); NSArray *array2 = [NSArray arrayWithContentsOfFile:filename]; //从文件读取
[array2 makeObjectsPerformSelector:@selector(print)];
}
}

  4)在数组中查找元素

  

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhangsan"];
NSString *str = [array1 objectAtIndex:]; //查找指定下标的对象
NSString *str2 = array1[];
NSLog(@"str = %@,str2 = %@",str,str2); BOOL ishas = [array1 containsObject:@"wangwu"]; //按指定值查找
if(ishas)
NSLog(@"有");
else
NSLog(@"没有"); NSString *str3 = [array1 firstObject]; //获取array中的第1个数据
NSString *str4 = [array1 lastObject]; //获取array中的最后的数据
NSLog(@"str3 = %@,str4 = %@",str3,str4); NSUInteger index = [array1 indexOfObject:@"zhangsan"]; //按值找下标,第1个
NSLog(@"index = %li",index); NSUInteger index2 = [array1 indexOfObject:@"zhangsan" inRange:NSMakeRange(index + , [array1 count] - )]; //在指定范围按值查找下标
NSLog(@"index2 = %li",index2);
}
}

  5)数组的添加和删除,注意这里的添加和删除并不会对原有数组产生影响,因为它是不可变的,修改后会生成一个新的数组。

  

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"];
NSArray *array2 = [array1 arrayByAddingObject:@"zhaoliu"]; //将某个对象加入到数组
NSLog(@"array2 = %@",array2); NSArray *array3 = @[@"aaa",@"bbb",@"ccc"];
NSArray *array4 = [array1 arrayByAddingObjectsFromArray:array3]; //将另一个数组数据加入到一个数组中,组成一个新的数组
NSLog(@"array4 = %@",array4); // array1[0] = @"abc"; //不可变数组不能删除
NSArray *array5 = [array4 subarrayWithRange:NSMakeRange(,)]; //获得array指定范围的子数组
NSLog(@"array5 = %@",array5);
}
}

  6)数组的排序

  

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
-(NSInteger)cmpstuname:(Student *)stu;
-(NSInteger)cmpstuage:(Student *)stu;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} -(NSInteger)cmpstuname:(Student *)stu{
return [self._name compare:stu._name];
} -(NSInteger)cmpstuage:(Student *)stu{
return self._age - stu._age;
}
@end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhangsan"];
NSLog(@"before sort array1 = %@",array1); NSLog(@"-------------------");
NSArray *array2 = [array1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"after sort array2 = %@",array2); NSLog(@"-------------------");
NSArray *array3 = [array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
NSLog(@"after sort array3 = %@",array3); NSLog(@"-------------------");
Student *stu1 = [[Student alloc] init:@"zhangsan" andage:];
Student *stu2 = [[Student alloc] init:@"lisi" andage:];
Student *stu3 = [[Student alloc] init:@"wangwu" andage:];
NSArray *stuarray = @[stu1,stu2,stu3];
// NSArray *srotstuarr = [stuarray sortedArrayUsingSelector:@selector(cmpstuname:)]; //按名排序
NSArray *srotstuarr = [stuarray sortedArrayUsingSelector:@selector(cmpstuage:)]; //按年龄排序
for(Student *temp in srotstuarr)
NSLog(@"name = %@,age = %i",temp._name,temp._age); NSLog(@"----------------");
NSArray *sortstuarr2 = [stuarray sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student * obj2) {
return [obj1._name compare:obj2._name];
}];
for(Student *temp in sortstuarr2)
NSLog(@"name = %@,age = %i",temp._name,temp._age); NSLog(@"----------------");
Student *stu4 = [[Student alloc] init:@"zhangsan" andage:];
Student *stu5 = [[Student alloc] init:@"lisi" andage:];
Student *stu6 = [[Student alloc] init:@"wangwu" andage:];
NSArray *stuarray2 = @[stu4,stu5,stu6];
NSArray *allstuarray = [stuarray arrayByAddingObjectsFromArray:stuarray2];
for(Student *temp in allstuarray)
NSLog(@"name = %@,age = %i",temp._name,temp._age); //使用描述符排序
NSSortDescriptor *namescr = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:NO]; //第1排序为按名降序排序
NSSortDescriptor *agescr = [NSSortDescriptor sortDescriptorWithKey:@"_age" ascending:YES]; //第2排序为按年龄升序排序
NSArray *sortallstu = [allstuarray sortedArrayUsingDescriptors:@[namescr,agescr]];
NSLog(@"-------------- ");
for(Student *temp in sortallstu)
NSLog(@"name = %@,age = %i",temp._name,temp._age);
}
}

  7)分割和连接

  

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} @end int main(int argc,char **argv){
@autoreleasepool {
NSString *path = @"/Users/user/workspace/OCTest/OCTest";
NSArray *patharray = [path componentsSeparatedByString:@"/"]; //将字符串按指定分隔符进行分割,分割形成数组
NSLog(@"patharray = %@",patharray); NSString *str = [patharray componentsJoinedByString:@"-"]; //将数组中的数组用指定的分隔符连接成字符串
NSLog(@"str = %@",str);
}
}

2. 可变字符串 (NSMutable)

  因为它是 NSArray 的子类,所以它继承了 NSArray 中的一些方法,所以下面只介绍它的常用的方法构建和修改。首先先看一下它的方法:

  

  下面通过一个例子来具体看它的用法:

  1)构造

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} @end int main(int argc,char **argv){
@autoreleasepool {
// NSMutableArray *marray = @[@"zhangsan",@"lisi",@"wangwu"]; //错误,类型不一致
NSMutableArray *marray = [NSMutableArray arrayWithCapacity:];
marray.array = @[@"zhangsan",@"lisi",@"wangwu"];
for(NSString *temp in marray)
NSLog(@"temp = %@",temp); NSLog(@"----------------");
NSArray *array = @[@"zhangsan",@"lisi",@"wangwu"];
NSMutableArray *marray2 = [NSMutableArray arrayWithArray:array]; //使用一个不可数组直接构造可变数组
for(NSString *temp in marray2)
NSLog(@"temp = %@",temp); NSLog(@"----------------");
NSMutableArray *marray3 = [[NSMutableArray alloc] initWithArray:array];
for(NSString *temp in marray3)
NSLog(@"temp = %@",temp);
}
}

  2)修改

  

 #import <Foundation/Foundation.h>

 @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} @end int main(int argc,char **argv){
@autoreleasepool {
NSMutableArray *marray = [NSMutableArray array];
[marray addObject:@"zhangsan"]; //将指定对象添加到数组 NSArray *array = @[@"lisi",@"wangwu"];
[marray addObjectsFromArray:array]; //将指定数组添加数组
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray insertObject:@"zhaoli" atIndex:]; //向指定下标添加数据
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeLastObject]; //删除最后一个
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeObjectAtIndex:]; //删除指定下标
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeObject:@"zhangsan"]; //按值删除
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray addObject:@"aaa"];
[marray addObject:@"bbb"];
[marray removeObjectsInRange:NSMakeRange(, )]; //将指定范围的数据删除
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray addObject:@"aaa"];
[marray addObject:@"lisi"];
[marray removeObject:@"lisi" inRange:NSMakeRange(,)]; //从指定范围删除指定对象
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeAllObjects]; //删除所有数据
NSLog(@"marray = %@",marray); }
}

  

  

  

  

Objective-c 数组对象的更多相关文章

  1. 了解JavaScript 数组对象及其方法

    数组在我目前学习过的编程语言中都可以见到, 形形色色的方法也数不胜数, 不过功能都一样, 最多也就是方法名稍稍有所不同, 老外也没个准啊, 如果英语比较好的同学对于学习方法(method)来说是很快的 ...

  2. 简述JavaScript对象、数组对象与类数组对象

    问题引出 在上图给出的文档中,用JavaScript获取那个a标签,要用什么办法呢?相信第一反应一定是使用document.getElementsByTagName('a')[0]来获取.同样的,在使 ...

  3. java数组对象的浅层复制与深层复制

    实际上,java中数组对象的浅层复制只是复制了对象的引用(参考),而深层复制的才是对象所代表的值.

  4. MongoDB学习笔记~官方驱动嵌套数组对象的更新

    回到目录 对于数组对象mongodb本身是支持的,不过对于数组的更新,mongodb的Csharp驱动目前只支持一级,即你的对象里包含数组,而数组又包括数组,这表示两层,这在更新子数组时,Csharp ...

  5. jquery解析php通过ajax传过来的json二维数组对象

    ajax获得php传过来的json二维数组对象,jquery解析 php代码: <?php $news = array( '武汉'=>array(1,2,3), '广州'=>arra ...

  6. Array.prototype.sort()对数组对象排序的方法

    Array.prototype.sort()方法接受一个参数——Function,Function会提供两个参数,分别是两个进行比较的元素,如果元素是String类型则通过Unicode code进行 ...

  7. MVVM架构~knockoutjs系列之为Ajax传递Ko数组对象

    返回目录 一些要说的 这是一个很有意思的题目,在KO里,有对象和数组对象两种,但这两种对象对外表现都是一个function,如果希望得到他的值,需要进行函数式调用,如ko_a(),它的结果为一个具体值 ...

  8. 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用

    [源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, con ...

  9. javascript数组对象排序

    javascript数组对象排序 JavaScript数组内置排序函数 javascript内置的sort函数是多种排序算法的集合 JavaScript实现多维数组.对象数组排序,其实用的就是原生的s ...

  10. JavaScript 中有关数组对象的方法

    JS 处理数组多种方法 js 中的数据类型分为两大类:原始类型和对象类型. 原始类型包括:数值.字符串.布尔值.null.undefined 对象类型包括:对象即是属性的集合,当然这里又两个特殊的对象 ...

随机推荐

  1. restful_api

    http://www.ruanyifeng.com/blog/2014/05/restful_api.html

  2. substr,substring,slice 的区别

    javascript中的三个函数substr,substring,slice都可以用来提取字符串的某一部分(函数名称都是小写,不要写成subStr,subString又或者Substring,记住js ...

  3. SurfaceView 和 View 区别

    android.view.View 和 android.view.SurfaceView SurfaceView 是从 View 基类中派生出来的显示类,直接子类有 GLSurfaceView和Vid ...

  4. JavaScript 开发经验整理

    前言 今年接触了一个B/S的项目,总结了一些JavaScript开发经验,整理些有用的内容与大家分享. 本文会持续更新... 1.实现代码访问的控制 随着项目JavaScript代码库扩大,本应被控制 ...

  5. Spring——jar包详解

    org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spring 2.5.6的时候需要a ...

  6. CentOS 6.2图形界面自定义开启终端快捷键

    菜单: System->Preferences->Keyboard Shortcuts 在Desktop分类下找到"Run a terminal" 点击Run a te ...

  7. Oracle查询表结构的常用语句

    1. 查询表结构基本信息 select * from user_tables t,user_tab_comments c where c.table_name = t.table_name and t ...

  8. 几个SQL

    select sum(`value`) from testtable where value != 'error' AND type ='b' in (select DISTINCT(type) fr ...

  9. 交换机access和trunk的一些小结(转)

     以太网端口有 3种链路类型:access.trunk.hybird Access类型端口只能属于1个VLAN 般用于连接计算机 端口: Trunk类型端口可以允许多个VLAN通过,可以接收和发送多个 ...

  10. JavaScript中的构造函数

    目录: constructor & prototype 为构造函数添加属性和方法的多种不同方法 组合使用this和prototype关键字创建构造函数(常用方法) 用对象直接量作为构造函数的参 ...