Object-c学习之路八(NSArray(数组)遍历和排序)
2013-07-25 19:40
423 查看
今天学习了NSArray的遍历和排序,现在在这里做一下总结:
遍历现在实现了四中方法:
排序大概有三中方法:(代码中都有注释)
关于对象的排序还是以Student和Book为例 每个Student持有一个Book.
主函数:
//
// main.m
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#pragma mark 创建一个集合
void arrayTest(){
//创建一个数组
NSArray *array=[NSArray array];
array=[NSArray arrayWithObjects:@"123",@"sf",@"dsg", nil];
NSLog(@"array is:%@",array);
NSLog(@"tength is: %zi",array.count);
}
#pragma mark 给数组里的元素发送消息
void arrayMessage(){
Student *stu1=[Student student];
Student *stu2=[Student student];
Student *stu3=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3, nil];
//让数组里的元素调用addMessage方法
[array makeObjectsPerformSelector:@selector(addMessage:) withObject:@"你好。"];
}
#pragma mark 数组遍历(四种方法)
void arrayLoop(){
Student *stu1=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,@"1",@"2",@"3", nil];
//方法一 for循环
for (int i=0; i<array.count; i++) {
NSLog(@"%i->%@",i,[array objectAtIndex:i]);
}
//方法二
int i=0;
for (id obj in array) {
NSLog(@"%i->%@",i,obj);
i++;
}
//方法三 通过Block遍历:官方建议使用block
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zi->%@",idx,obj);
//当执行到第3个时停止
if (idx==2){
*stop=YES;
}
}];
//方法四 通过迭代器遍历
//获得一个迭代器
NSEnumerator *enumerator=[array objectEnumerator];
id next=nil;
while (next=[enumerator nextObject]) {
NSLog(@"next->%@",next);
}
//反向遍历
NSEnumerator *rvenumerator=[array reverseObjectEnumerator];
id rvnext=nil;
while (rvnext=[rvenumerator nextObject]) {
NSLog(@"rvnext->%@",rvnext);
}
}
#pragma mark 数组排序
void arraySort(){
NSArray *array=[NSArray arrayWithObjects:@"4",@"1",@"2",@"3", nil];
//排完后产生一个新的数组
//指定元素的排序方法 compare:
NSArray *array2=[array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后:%@",array2);
}
#pragma mark 对数组中的Student对象进行排序:
//先按照性进行排序,后按照名进行排序(两种方法)
void studentSort(){
Student *stu1=[Student initWithFirstName:@"lianjie" LastName:@"Li"];
Student *stu2=[Student initWithFirstName:@"dehua" LastName:@"Liu"];
Student *stu3=[Student initWithFirstName:@"xingle" LastName:@"Li"];
Student *stu4=[Student initWithFirstName:@"long" LastName:@"Cheng"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//第一种方法
NSArray *array2=[array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"排序后:%@",array2);
//第二种方法 通过Block排序
NSArray *array3=[array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
//先按照性进行排序
NSComparisonResult result=[obj1.lastname compare:obj2.lastname];
//再按照名进行排序
if (result==NSOrderedSame) {
result=[obj1.firstname compare:obj2.firstname];
}
return result;
}];
NSLog(@"通过Block排序后:%@",array3);
}
#pragma mark 对数组中的Student对象进行排序:
//对数组中的Student对象进行排序:先按照性进行排序,后按照名进行排序(另一种方法)
void studentSort2(){
Student *stu1=[Student initWithFirstName:@"lianjie" LastName:@"Li" bookName:@"book1"];
Student *stu2=[Student initWithFirstName:@"dehua" LastName:@"Liu" bookName:@"book2"];
Student *stu3=[Student initWithFirstName:@"xingle" LastName:@"Li" bookName:@"book2"];
Student *stu4=[Student initWithFirstName:@"long" LastName:@"Cheng" bookName:@"book1"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//先按照书名进行排序
NSSortDescriptor *bookNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
NSSortDescriptor *lastnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
NSSortDescriptor *firstnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
NSArray *decarray=[NSArray arrayWithObjects:bookNameDesc,lastnameDesc,firstnameDesc, nil];
NSArray *array2=[array sortedArrayUsingDescriptors:decarray];
NSLog(@"排序后:%@",array2);
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
// arrayTest();
// arrayMessage();
//arrayLoop();
// arraySort();
// studentSort();
//先根据book名,后根据性 ,再根据名 进行排序
studentSort2();
}
return 0;
}
Student函数:
头文件:
// Student.h
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject{
NSString *_firstname;
NSString *_lastname;
}
@property (nonatomic,retain) NSString * firstname;
@property (nonatomic,retain) NSString * lastname;
//每个学生拥有一本书
@property (nonatomic,retain)Book *book;
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname;
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname bookName:(NSString *)bookName;
+(id)student;
-(void) addMessage:(NSString *)str;
-(NSComparisonResult)compareStudent:(Student *)stu;
@end.m文件:
Book类:
.h文件:
// Book.h
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@property (nonatomic ,retain) NSString *name;
+(id)initBookWithName:(NSString *)name;
@end
.m文件:
![](http://img.blog.csdn.net/20130725192953187?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193005593?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193016859?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193038406?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193052312?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193102859?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193114687?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193125734?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130725193136265?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VwZXJsZWxlMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
遍历现在实现了四中方法:
排序大概有三中方法:(代码中都有注释)
关于对象的排序还是以Student和Book为例 每个Student持有一个Book.
主函数:
//
// main.m
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#pragma mark 创建一个集合
void arrayTest(){
//创建一个数组
NSArray *array=[NSArray array];
array=[NSArray arrayWithObjects:@"123",@"sf",@"dsg", nil];
NSLog(@"array is:%@",array);
NSLog(@"tength is: %zi",array.count);
}
#pragma mark 给数组里的元素发送消息
void arrayMessage(){
Student *stu1=[Student student];
Student *stu2=[Student student];
Student *stu3=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3, nil];
//让数组里的元素调用addMessage方法
[array makeObjectsPerformSelector:@selector(addMessage:) withObject:@"你好。"];
}
#pragma mark 数组遍历(四种方法)
void arrayLoop(){
Student *stu1=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,@"1",@"2",@"3", nil];
//方法一 for循环
for (int i=0; i<array.count; i++) {
NSLog(@"%i->%@",i,[array objectAtIndex:i]);
}
//方法二
int i=0;
for (id obj in array) {
NSLog(@"%i->%@",i,obj);
i++;
}
//方法三 通过Block遍历:官方建议使用block
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zi->%@",idx,obj);
//当执行到第3个时停止
if (idx==2){
*stop=YES;
}
}];
//方法四 通过迭代器遍历
//获得一个迭代器
NSEnumerator *enumerator=[array objectEnumerator];
id next=nil;
while (next=[enumerator nextObject]) {
NSLog(@"next->%@",next);
}
//反向遍历
NSEnumerator *rvenumerator=[array reverseObjectEnumerator];
id rvnext=nil;
while (rvnext=[rvenumerator nextObject]) {
NSLog(@"rvnext->%@",rvnext);
}
}
#pragma mark 数组排序
void arraySort(){
NSArray *array=[NSArray arrayWithObjects:@"4",@"1",@"2",@"3", nil];
//排完后产生一个新的数组
//指定元素的排序方法 compare:
NSArray *array2=[array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后:%@",array2);
}
#pragma mark 对数组中的Student对象进行排序:
//先按照性进行排序,后按照名进行排序(两种方法)
void studentSort(){
Student *stu1=[Student initWithFirstName:@"lianjie" LastName:@"Li"];
Student *stu2=[Student initWithFirstName:@"dehua" LastName:@"Liu"];
Student *stu3=[Student initWithFirstName:@"xingle" LastName:@"Li"];
Student *stu4=[Student initWithFirstName:@"long" LastName:@"Cheng"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//第一种方法
NSArray *array2=[array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"排序后:%@",array2);
//第二种方法 通过Block排序
NSArray *array3=[array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
//先按照性进行排序
NSComparisonResult result=[obj1.lastname compare:obj2.lastname];
//再按照名进行排序
if (result==NSOrderedSame) {
result=[obj1.firstname compare:obj2.firstname];
}
return result;
}];
NSLog(@"通过Block排序后:%@",array3);
}
#pragma mark 对数组中的Student对象进行排序:
//对数组中的Student对象进行排序:先按照性进行排序,后按照名进行排序(另一种方法)
void studentSort2(){
Student *stu1=[Student initWithFirstName:@"lianjie" LastName:@"Li" bookName:@"book1"];
Student *stu2=[Student initWithFirstName:@"dehua" LastName:@"Liu" bookName:@"book2"];
Student *stu3=[Student initWithFirstName:@"xingle" LastName:@"Li" bookName:@"book2"];
Student *stu4=[Student initWithFirstName:@"long" LastName:@"Cheng" bookName:@"book1"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//先按照书名进行排序
NSSortDescriptor *bookNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
NSSortDescriptor *lastnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
NSSortDescriptor *firstnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
NSArray *decarray=[NSArray arrayWithObjects:bookNameDesc,lastnameDesc,firstnameDesc, nil];
NSArray *array2=[array sortedArrayUsingDescriptors:decarray];
NSLog(@"排序后:%@",array2);
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
// arrayTest();
// arrayMessage();
//arrayLoop();
// arraySort();
// studentSort();
//先根据book名,后根据性 ,再根据名 进行排序
studentSort2();
}
return 0;
}
Student函数:
头文件:
// Student.h
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject{
NSString *_firstname;
NSString *_lastname;
}
@property (nonatomic,retain) NSString * firstname;
@property (nonatomic,retain) NSString * lastname;
//每个学生拥有一本书
@property (nonatomic,retain)Book *book;
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname;
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname bookName:(NSString *)bookName;
+(id)student;
-(void) addMessage:(NSString *)str;
-(NSComparisonResult)compareStudent:(Student *)stu;
@end.m文件:
// // Student.m // NSArray // // Created by WildCat on 13-7-25. // Copyright (c) 2013年 wildcat. All rights reserved. // #import "Student.h" #import "Book.h" @implementation Student @synthesize firstname=_firstname; @synthesize lastname=_lastname; +(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname{ Student *stu=[[[Student alloc] init] autorelease]; stu.firstname=firstname; stu.lastname=lastname; return stu; } +(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname bookName:(NSString *)bookName{ Student *stu=[Student initWithFirstName:firstname LastName:lastname]; Book *book=[Book initBookWithName:bookName]; stu.book=book; return stu; } +(id)student{ return [[[Student alloc] init] autorelease]; } -(void) addMessage:(NSString *)str{ NSLog(@"%@->addMessage->%@",self,str); } -(void)dealloc{ [_lastname release]; [_firstname release]; NSLog(@"%@ 被销毁",self); [super dealloc]; } //定义一个排序方法 -(NSComparisonResult)compareStudent:(Student *)stu{ //先按照性进行排序 NSComparisonResult result=[self.lastname compare:stu.lastname]; //再按照名进行排序 if (result==NSOrderedSame) { result=[self.firstname compare:stu.firstname]; } return result; } //重定义description 方法 -(NSString *)description{ return [NSString stringWithFormat:@"%@ %@->%@",self.lastname,self.firstname,self.book.name]; } @end
Book类:
.h文件:
// Book.h
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@property (nonatomic ,retain) NSString *name;
+(id)initBookWithName:(NSString *)name;
@end
.m文件:
#import "Book.h" @implementation Book +(id)initBookWithName:(NSString *)name{ Book *book=[[[Book alloc] init] autorelease]; book.name=name; return book; } -(void)dealloc{ [_name release]; [super dealloc]; } @end
相关文章推荐
- Object-c学习之路八(NSArray(数组)遍历和排序)
- [oc学习笔记]数组的遍历和排序
- OC-NSArray数组的三种遍历方式和排序方法
- PHP学习(15) 数组的遍历和排序
- [oc学习笔记]数组的遍历和排序
- OC学习第五天_字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序
- 【学习ios之路:C语言】一维数组,数组排序,字符数组
- 【小白笔记】PHP学习之路 (19) --数组增改排序
- 【小白笔记】PHP学习之路 (18) --数组遍历、统计、计算
- NSArray数组的常用操作(遍历、排序、倒序、比较、与字符串的比较)
- NO4.java学习笔记(数组、数组的操作【遍历、排序、查找】)
- [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组
- c++ 模板学习笔记:函数模板实现数组通用排序和遍历打印(权哥)
- java学习之路 之 面向对象编程-main方法的语法、数组排序、操作数组的工具类、数组操作常见问题
- Object-C学习笔记3——数组、字典
- Oracle学习之路(一):oracle简介+基本sql语句+条件查询+排序数据理论与案例
- 数组(遍历、转置、元素替换、排序、复制)
- 【JavaSE_学习笔记】排序、二分查找与数组工具类
- 黑马程序员——Java学习笔记——数组的排序和查找
- JavaSE 学习参考:数组遍历