//声明一个数组
NSString *strOne=@"lxd";
NSString *strTow=@"abc";
NSString *strTree=@"def";
NSArray *strArray=[[NSArray alloc] initWithObjects:strOne,strTow,strTree, nil];//数组初始化方法
// 查看数组里面存了几个 strArray.count;
NSLog(@"%d",strArray.count); //.count 是元素中元素的个数
// NSString * str14= [strArray objectAtIndex:1];
NSString * str14=(NSString *)[strArray objectAtIndex:1];//强转字符串 objectAtIndex:获取指定Index的对象 索引
NSLog(@"%@",str14);
for (int i=0; i<strArray.count; i++) //依次输出每个值 objectAtIndex:获取指定Index的对象; .count 是元素中元素的个数
{
NSLog(@"%@",(NSString *)[strArray objectAtIndex:i]);
}
//创建三个对象
Student *s1=[[Student alloc] initWithName:@"mellen"];
Student *s2=[[Student alloc]initWithName:@"jobs"];
Student *s3=[[Student alloc]initWithName:@"billgates"];
//往数组里存
NSArray *stuArray=[[NSArray alloc]initWithObjects:s1,s2,s3, nil];
//第一种遍历方式
for (int i=0; i<stuArray.count; i++)
{
Student *stu=(Student *)[stuArray objectAtIndex:i];
[stu showInfo];
}
[s1 release];
[s2 release];
[s3 release];
//是第二种遍历方式
for (Student*s in stuArray) //语法
{
[s showInfo];
}
//可变数组
Student *s4=[[Student alloc] initWithName:@"aaaaa"];
Student *s5=[[Student alloc]initWithName:@"bbbb"];
Student *s6=[[Student alloc]initWithName:@"ccccc"];
NSMutableArray *stuMutableArray=[[NSMutableArray alloc]initWithCapacity:0];
[stuMutableArray addObject:s4];//添加对象 addobject
[stuMutableArray addObject:s5];
[stuMutableArray addObject:s6];
[s4 release];
[s5 release];
[s6 release];
[stuMutableArray removeObject:s4];//删除s4
for (Student*s in stuMutableArray)
{
[s showInfo];
}
[stuMutableArray release];
NSArray 大杂烩
最新推荐文章于 2019-05-11 10:45:51 发布