首页 > 代码库 > oc集合
oc集合
本人之前学习过一年半ios开发 由于行情太过凄惨,故转前端。心在前端,苹果亦难忘!把我平时的笔记作出给大家总结!
NSMutableArray *arrstu=[NSMutableArray arrayWithCapacity:10];
student *stu1=[[student alloc]init];
stu1.name=@"我";
student *stu2=[[student alloc]init];
stu2.name=@"少";
student *stu3=[[student alloc]init];
stu3.name=@"帅";
[arrstu addObject:stu1];
[arrstu addObject:stu2];
[arrstu addObject:stu3];
[arrstu insertObject:@"兵" atIndex:2];//插入
[arrstu removeObjectAtIndex:3];//删除
int count ;
[arrstu replaceObjectAtIndex:2 withObject:@"youxi"];//替换
for(id obj in arrstu)
{
NSLog(@"数组对象%d:%@",count,obj);
count++;
}
1.编写程序,以数组存储字符串”元芳”、”华生”、”平次”,并用循环语句输出数组中存储的所有字符串。
尝试使用可变数组版本进行程序扩展,可以存放不定项个字符串。
NSArray *array = [NSArray arrayWithObjects:@"元芳", @"华生", @"平次", nil];
//使用普通循环遍历数组
NSLog(@"使用普通循环遍历数组:");
for (int i=0; i<[array count]; i++) {
NSLog(@"%@",[array objectAtIndex:i]);
}
//使用枚举器遍历数组
NSLog(@"使用枚举器遍历数组:");
NSEnumerator *objEnumerator = [array objectEnumerator];
id obj = NULL;
while (obj = [objEnumerator nextObject]) {
NSLog(@"%@",obj);
}
//使用快速枚举遍历数组
NSLog(@"使用快速枚举遍历数组:");
for (id obj in array) {
NSLog(@"%@",obj);
}
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:3];
//可变数组中存放元素个数并不受arrayWithCapacity参数值限制,那只是一个最优值
[mArray addObject:@"元芳"];
[mArray addObject:@"华生"];
[mArray addObject:@"平次"];
[mArray addObject:@"何瑾"];
[mArray addObject:@"习禁苹"];
//使用快速枚举遍历可变数组
NSLog(@"使用快速枚举遍历可变数组:");
for (id obj in mArray) {
NSLog(@"%@",obj);
}
2.编写程序,以可变数组存储1到10之间的不重复的随机整数,再对这个数组进行从小到大排序,最后用循环语句输出所有值。
@autoreleasepool {
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:10];
NSNumber *number = nil;
int i = 0,j = 0,tmp = 0,value[10] = {0};
BOOL repeat = NO;
//获取1到10之间的不重复的随机整数放入value[10]数组中
for (i=0; i<<span class="s3">10; i++) {
repeat = NO;
tmp = (arc4random() % 10) + 1;//获取1到10之间的随机整数
//以下算法确保生成的随机数不重复
for (j=0; j
if (tmp == value[j]) {
repeat = YES;//重复了
break;
}
}
if (repeat) {
i -= 1;
} else {
value[i] = tmp;
}
}
//装箱1到10之间的随机整数
for (i=0; i<<span class="s3">10; i++) {
number = [NSNumber numberWithInt:value[i]];//装箱1到10之间的随机整数
[mArray addObject:number];//放入可变数组中
}
NSLog(@"排序前:");
for (NSNumber *number in mArray) {
NSLog(@"%d",[number intValue]);
}
[mArray sortUsingSelector:@selector(compare:)];//排序(NSNumber继承NSObject ,可以使用比较compare: isEqual等消息)
NSLog(@"排序后:");
for (NSNumber *number in mArray) {
NSLog(@"%d",[number intValue]);
}
}
5.放四个十进制三位数到一个数组中,然后按从小到大排序后组成一个新的数组
3.编写一个学生类,要求学生类有特征:姓名,年龄,性别,学号,成绩,然后分别根据姓名,年龄,成绩进行排序
//编写一个学生类,要求学生类有特征:姓名,年龄,性别,学号,成绩,然后分别根据姓名,年龄,成绩进行排序
Student * stu_one = [[Student alloc]init];
stu_one.name = @"唐僧";
stu_one.age = 25;
stu_one.score = 100;
stu_one.stuID = 1;
Student * stu_two = [[Student alloc]init];
stu_two.name = @"孙悟空";
stu_two.age = 30;
stu_two.score = 95;
stu_two.stuID = 2;
Student * stu_three = [[Student alloc]init];
stu_three.name = @"孙悟空";
stu_three.age = 35;
stu_three.score = 90;
stu_three.stuID = 3;
NSArray * stuArray = [[NSArray alloc]initWithObjects:stu_one,stu_two,stu_three, nil];
//使用不同的排序方式只需要修改关键字
NSSortDescriptor * sd_stu = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray * sdArray = [NSArray arrayWithObject:sd_stu];
NSArray * stuArray_last = [stuArray sortedArrayUsingDescriptors:sdArray];
for(id obj in stuArr)
{
NSLog(@"%@",obj);
}
/////////////////////////////////////////////////////////////////////////////////////
@interface Student : NSObject
@property NSString * name;
@property int age;
@property NSString * sex;
@property int stuID;
@property int score;
@end
#import "Student.h"
@implementation Student
-(NSString *)description
{
NSString *string=[NSString stringWithFormat:@"大家好我叫%@,今年%d岁了,性别%@,学号:%d,成绩:%d",_name,_age,_sex,_num,_score];
return string;
}
@end
4.使用可变数组编写一个学生管理系统,可以任意添加,删除,修改 学生的姓名
使用控制台输入数字决定当前要执行的操作,如 1、添加学生姓名 2、删除学生姓名、3.修改学生姓名
int count;
NSMutableArray *arrstu=[NSMutableArray arrayWithCapacity:10];
Student *stu1=[[Student alloc]init];
stu1.name=@"少";
Student *stu2=[[Student alloc]init];
stu2.name=@"帅";
Student *stu3=[[Student alloc]init];
stu3.name=@"我";
[arrstu addObject:stu1];
[arrstu addObject:stu2];
[arrstu addObject:stu3];
int a;
printf("请输入,你需要的内容:1、添加学生姓名 2、删除学生姓名、3.修改学生姓名");
scanf("%d",&a);
switch (a) {
case 1:
[arrstu insertObject:@"兵" atIndex:2];
break;
case 2:
[arrstu removeObjectAtIndex:1];//删除
break;
case 3:
[arrstu replaceObjectAtIndex:2 withObject:@"youxi"];//替换
break;
default:
break;
}
for(id obj in arrstu)
{
NSLog(@"数组对象%d:%@",count,obj);
count++;
}
2.
NSString *str1=@"123-456-789-000";
NSArray *newArray=[str1 componentsSeparatedByString:@"-"];
NSLog(@"%@",newArray);
NSMutableString *newStr=[NSMutableString stringWithCapacity:10];
for (NSString *s in newArray)// s是指向元素
{
[newStr appendString:s];
}
NSLog(@"newStr =%@",newStr);
字典:
//用NSDictionary方法
NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:@"元芳",@"狄仁杰",@"展昭",@"包拯",@"毛利",@"柯南",nil];
for (NSString *key in [dic allKeys]) {
NSString *value=http://www.mamicode.com/[dic valueForKey:key];
NSLog(@"%@问%@这事你怎么看",key,value);
}
//用NSMutableDictionary方法
NSMutableDictionary *zidian=[[NSMutableDictionary alloc]init];
detective *det1=[[detective alloc]init];
det1.name=@"元芳";
detective *det2=[[detective alloc]init];
det2.name=@"展昭";
detective *det3=[[detective alloc]init];
det3.name=@"毛利";
[zidian setObject:det1 forKey:@"狄仁杰"];
[zidian setObject:det2 forKey:@"包拯"];
[zidian setObject:det3 forKey:@"柯南"];
for (NSString *key in [zidian allKeys]) {
NSString *value=http://www.mamicode.com/[zidian valueForKey:key];
NSLog(@"%@问%@这事你怎么看!",key,value);
}
int num1=333;
int num2=231;
int num3=158;
int num4=998;
NSMutableArray *arr1=[NSMutableArray arrayWithCapacity:5];
NSNumber *num_1 =[NSNumber numberWithInt:num1];
NSNumber *num_2 =[NSNumber numberWithInt:num2];
NSNumber *num_3 =[NSNumber numberWithInt:num3];
NSNumber *num_4 =[NSNumber numberWithInt:num4];
[arr1 addObject: num_1];
[arr1 addObject: num_2];
[arr1 addObject: num_3];
[arr1 addObject: num_4];
[arr1 sortUsingSelector:@selector(compare:)];
for (id obj in arr1) {
NSLog(@"%@",obj);
}
//取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出,(提示:可变字符串;返回数组)
NSString *str1=@"123-456-789-000";
NSArray *newArray=[str1 componentsSeparatedByString:@"-"];
NSLog(@"%@",newArray);
NSMutableString *newStr=[NSMutableString stringWithCapacity:10];
for (NSString *s in newArray)// s是指向元素
{
[newStr appendString:s];
}
NSLog(@"newStr =%@",newStr);
字典讲解:
///////////////////////////////////////////////////////////////////////////////////////
#import
#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student * stu_1 = [[Student alloc]init];
Student * stu_2 = [[Student alloc]init];
Student * stu_3 = [[Student alloc]init];
Student * stu_4 = [[Student alloc]init];
//使用不可变字典时,必须在创建时指定字典内元素
//注意:作为字典的关键字必须要遵循NSCopying协议
//字典内元素必须成对出现,元素个数永远只有偶数个
//字典中对象的关键字必须唯一
//不可变字典一经创建不可修改
//遍历字典时一定要注意遍历出来的元素顺序
NSDictionary * stuDic = [NSDictionary dictionaryWithObjectsAndKeys:stu_1,@"stu1",stu_2,@"stu2",stu_3,@"stu3",stu_4,@"stu4", nil];
NSLog(@"stuDic = %@",[stuDic objectForKey:@"stu3"]);
NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]init];
//为可变字典添加元素
[mutDic setObject:stu_1 forKey:@"stu1"];
[mutDic setObject:stu_2 forKey:@"stu2"];
NSLog(@"mutDic = %@",mutDic);
//删除可变字典指定元素
[mutDic removeObjectForKey:@"stu1"];
[mutDic setObject:stu_3 forKey:@"stu1"];
[mutDic setDictionary:stuDic];
NSLog(@"mutDic = %@",mutDic);
//集合的特点,元素无序
//元素唯一
//(自学NSMutbaleSet)
//NSNumber 是对C语言中数据类型的封装,可以将c数据类型转换为OC的对象,以方便存放到数组等集合当中
//数据一旦进行转换后,将不再支持算数运算,因为变成了对象
//注意明确转换前后的数据类型对应
int num1 = 65;
int num2 = 20;
NSNumber * intNum = [NSNumber numberWithInt:num1];
NSNumber * intNum2 = [NSNumber numberWithInt:num2];
int tmpint = [intNum intValue];
NSLog(@"%d",tmpint);
NSLog(@"%@",intNum);
//NSValue
NSRect rect = NSMakeRect(0, 0, 320, 480);
NSValue * rectValue = http://www.mamicode.com/[NSValue valueWithBytes:&rect objCType:@encode(NSRect)];
NSSize size = NSMakeSize(20, 20);
NSValue * sizeValue = http://www.mamicode.com/[NSValue valueWithSize:size];
NSLog(@"rectValue = http://www.mamicode.com/%@",rectValue);
NSLog(@"sizeValue = http://www.mamicode.com/%@",sizeValue);
//使用getValue取值时,参数是要存放值得变量
NSSize tmpsize;
[sizeValue getValue:&tmpsize];
NSLog(@"%f",tmpsize.width);
//(自学NSNull)
//创建空对象,主要用途,数组占位
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
#import
@interface Student : NSObject
@end
///////////////////////////////////////////////////////////////////////////////////////
#import "Student.h"
@implementation Student
@end
///////////////////////////////////////////////////////////////////////////////////////
oc集合