练习(数组)
来源:互联网 发布:c语言||什么意思 编辑:程序博客网 时间:2024/04/28 04:52
Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject{
NSString *name;
int age;
}
@property (copy,nonatomic) NSString *name;
@property int age;
-(void) print;
-(NSComparisonResult) compareName:(id) element;
@end
Student.m
#import "Student.h"
@implementation Student
@synthesize name,age;
-(void) print{
NSLog(@"name is %@,age is %i",name,age);
}
-(NSComparisonResult) compareName:(id)element{
return [name compare:[element name]];
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Student.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
/*NSArray *city = [NSArray arrayWithObjects:@"北京",@"上海",@"杭州",@"苏州",@"天津",@"武汉", nil];
// if ([city indexOfObject:@"杭州"] == NSNotFound) {
//NSLog(@"杭州未在其中");
//}else{
//NSLog(@"杭州身在其中");
//}
//for (int i=0; i < [city count]; i++) {
//NSLog(@"%@",[city objectAtIndex:i]);
}*/
//NSMutableArray *city = [[NSMutableArray alloc] init];
//[city addObject:@"北京"];
//[city addObject:@"上海"];
//[city addObject:@"杭州"];
//[city removeObjectAtIndex:1];
//NSMutableArray *nsma = [NSMutableArray arrayWithCapacity:20];
/*for (int p = 1; p <= 50; p++) {
if (p%3 == 0) {
[nsma addObject:[NSNumber numberWithInteger:p]];
}
}
for (int i = 0; i < [nsma count]; i++ ){
NSLog(@"%li",(long)[[nsma objectAtIndex:i] integerValue]);
}*/
Student *stu1 = [[Student alloc] init];
Student *stu2 = [[Student alloc] init];
Student *stu3 = [[Student alloc] init];
[stu1 setName:@"Sam"];
[stu1 setAge:30];
[stu2 setName:@"Lee"];
[stu2 setAge:23];
[stu3 setName:@"Alex"];
[stu3 setAge:26];
NSMutableArray *students = [[NSMutableArray alloc] init];
[students addObject:stu1];
[students addObject:stu2];
[students addObject:stu3];
NSLog(@"排序前");
for (int i=0; i < [students count]; i++) {
Student *stu4 = [students objectAtIndex:i];
NSLog(@"Name:%@,Age:%i",[stu4 name],[stu4 age]);
}
[students sortUsingSelector:@selector(compareName:)];
NSLog(@"排序后");
for (Student *stu4 in students) {
NSLog(@"Name:%@,Age:%i",stu4.name,stu4.age);
}
}
return 0;
}
- 练习(数组)
- 飞机座位(数组练习)
- C#数组(小练习)
- 数组练习
- 数组 练习
- 数组练习
- 数组练习
- 练习,数组
- 数组练习
- 数组练习
- 数组练习
- 数组练习
- 数组练习
- 数组练习
- 数组练习
- 【openjudge 计算概论(A)】[编程练习(数组练习)]
- java例程练习(一维数组)
- 数组引用做形参练习(数据结构test3)
- 基于Visual C++之Windows核心编程代码分析(4)获取操作系统信息
- 练习(字典)
- 让table的边线一样粗细的方法。
- Java实践之路(设计模式)——工厂模式(工厂方法)
- Web Service代理
- 练习(数组)
- 如何学习网页技术CSS
- 重载new和delete后调用构造函数和析构函数顺序
- 压缩感知系列科普文章之一:填补空缺——压缩感知
- vim
- 整型对象
- Linux世界开启传送门9-预备第1天
- 字节流转换为对象的方法
- Linux下配置VSftp服务器八步搞定