OC05_数组
来源:互联网 发布:swift 自定义元素数组 编辑:程序博客网 时间:2024/06/06 13:00
//
// main.m
// OC06_数组
//
// Created by dllo on 15/7/20.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Book.h"
int main(int argc,constchar * argv[]) {
// int arr[5]={3,2,1,4,5};
// for (int i=0; i<5-1; i++) {
// for (int j=0; j<5-1-i; j++) {
// if (arr[j]>arr[j+1]) {
// int temp=0;
// temp=arr[j];
// arr[j]=arr[j+1];
// arr[j+1]=temp;
// }
// }
//}
// for (int i=0; i<5; i++) {
// printf("%d ",arr[i]);
// }
//OC中的数组
//OC的数组里存放的一定是对象.
// NSArray *arr=[[NSArray alloc] init];
// NSArray *arr=[NSArray array];//便利构造器,没有参数,有参数时加with...
//1.字面量的方式创建一个数组
// NSArray *arr=@[@"4",@"2",@"3",@"1",@"5"];
// NSLog(@"%ld",arr.count);//数组里的元素个数
// NSLog(@"%ld个元素",[arr count]);
// //取值也是通过下标.返回的是一个对象.
// NSLog(@"%@",[arr objectAtIndex:2]);
// NSLog(@"%@",arr[2]);
//for循环进行数组遍历
// for (NSInteger i=0; i<arr.count; i++) {
// NSLog(@"%@",arr[i]);
// }
////不可变数组不能进行冒泡排序,,不能交换
// NSLog(@"%d",[arr containsObject:@"6"]);
// Student *stu1=[[Student alloc] initWithname:@"文世倾"];
// Student *stu2=[[Student alloc] initWithname:@"文世轩"];
// Student *stu3=[[Student alloc] initWithname:@"宁致远"];
// Student *stu4=[[Student alloc] initWithname:@"安乐颜"];
// NSArray *arr=[[NSArray alloc] initWithObjects:stu1,stu2,stu3,stu4, nil];
// for (NSInteger i=0; i<arr.count; i++) {
// Student *stu=arr[i];
// NSLog(@"%@",stu.name);
// }
// NSArray *arr1=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
// for (NSInteger i=0; i<arr.count; i++) {
// Student *stu=arr1[i];
// NSLog(@"%@",stu.name);
// }
//NSArray *arr=@[@"安逸臣",@"宁致远",@"文世倾",@"安乐颜"];
//NSLog(@"%@",arr);
//快速枚举:快速遍历数组等容器的对象.
//都是对容器里的每一个元素的遍历.
//为了增加代码的阅读性,避免不必要的错误,尽量让forin的前部分类型和数组里元素的类型相同.
// for (NSString *str in arr) {
// NSLog(@"%@",str);
// }
////数组嵌套
// NSArray *arr1=@[@"安逸臣",@"宁致远",@"文世倾",@"安乐颜"];
// NSArray *arr2=@[@"唐雪见",@"景天",@"紫萱",@"徐长卿"];
// NSArray *arr=@[arr1, arr2];
// //对arr进行forin遍历
// for (NSArray *temp in arr) {
// for (NSString *str in temp) {
// NSLog(@"%@",str);
// }
// }
// Student *stu1=[[Student alloc] initWithname:@"文世倾"];
// Student *stu2=[[Student alloc] initWithname:@"文世轩"];
// Student *stu3=[[Student alloc] initWithname:@"宁致远"];
// Student *stu4=[[Student alloc] initWithname:@"安乐颜"];
// NSArray *arr1=@[stu1,stu2,stu3];
// NSArray *arr2=@[stu4];
// NSArray *arr=@[arr1,arr2];
// //遍历数组每一个学生的姓名
// for (NSArray *temp in arr) {
// for (Student *p in temp) {
// NSLog(@"%@",p.name);
// }
// }
// NSArray *testarr=@[@"1",@"2",@"3"];
// NSArray *arr=[[NSArray alloc] initWithArray:testarr];
// NSArray *arr1=[[NSArray alloc]initWithObjects:testarr, nil];
// NSLog(@"%@",arr);
// NSLog(@"%@",arr1);
// NSLog(@"arr数组元素个数:%ld",arr.count);
// NSLog(@"arr1数组元素个数:%ld",[arr1 count]);
// NSArray *arr2=[NSArray arrayWithArray:arr];
//可变数组
//NSMutableArray *arr=[NSMutableArray array];//创建一个空的可变数组
//NSMutableArray *arr=[[NSMutableArray alloc] init];
// NSMutableArray *arr=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7", nil];
// ///添加一个字符串
// [arr addObject:@"anyichen"];
// NSLog(@"添加一个字符串:%@",arr);
// ///移除下标2的字符串
// [arr removeObjectAtIndex:2];
// NSLog(@"移除下标2的字符串:%@",arr);
// ///插入一个字符串
// [arr insertObject:@"ningzhiyuan" atIndex:2];
// NSLog(@"插入一个字符串:%@",arr);
// ///替换一个字符串
// [arr replaceObjectAtIndex:4 withObject:@"wenshiqing"];
// NSLog(@"替换一个字符串:%@",arr);
// ///交换两个字符串
// [arr exchangeObjectAtIndex:3 withObjectAtIndex:2];
// NSLog(@"交换两个字符串:%@",arr);
// ///清空数组
// [arr removeAllObjects];
// NSLog(@"清空数组:%@",arr);
//
Book *book1=[Book bookWithName:@"湘西秘史" price:45.9];
Book *book2=[Book bookWithName:@"西游记" price:73.6];
Book *book3=[Book bookWithName:@"红楼梦" price:45.5];
Book *book4=[Book bookWithName:@"水浒传" price:100];
NSMutableArray *bookArr=[NSMutableArray arrayWithObjects:book1,book2,book3,book4,nil];
//1.数组可以添加、删除书籍。*************************************
Book *book5=[Book bookWithName:@"四书" price:35.6];
[bookArr addObject:book5];//添加
[bookArr removeObject:book2];//删除
// Book *temp=[bookArr objectAtIndex:2];
////打印:
for (Book *strin bookArr) {
NSLog(@"书名:%@,价格:%g",str.name,str.price);
}
NSLog(@"\n");
//2.可以从数组根据书名查找书籍,并修改书籍的价格*********************
for (Book *qin bookArr) {
if ([q.name isEqualToString:@"红楼梦"]) {
q.price=90.99;
}
}
////打印:
for (int i=0; i<bookArr.count; i++){
Book *p=bookArr[i];
NSLog(@"书名:%@,价格:%g",p.name,p.price);
}
NSLog(@"\n");
//3.展示所有书籍清单(书名)****************************************
////法1.
for (int i=0; i<bookArr.count; i++){
Book *p=bookArr[i];
NSLog(@"%@",p.name);
}
NSLog(@"\n");
////法2.
for (Book *strin bookArr) {
NSLog(@"书名:%@,价格:%g",str.name,str.price);
}
NSLog(@"\n");
//4.找到价格在70到100有多少本书,然后把符合条件的书放在一个数组里***********
//一定要创建空数组
NSMutableArray *newArr=[NSMutableArray array];
for (Book *hin bookArr) {
if (h.price>=70&&h.price<=100) {
//打印满足条件的书籍
NSLog(@"%@,%g",h.name,h.price);
//将满足条件的书籍放于数组newArr中.
[newArr addObject:h];
}
}
NSLog(@"\n");
//遍历数组newArr
for (Book *strin newArr) {
NSLog(@"书名:%@,价格:%g",str.name,str.price);
}
NSLog(@"\n");
//5.找到价格是100的书,把书名改成"论语"********************************
for (Book *win bookArr) {
if (w.price==100) {
w.name=@"论语";
}
}
for (Book *strin bookArr) {
NSLog(@"书名:%@,价格:%g",str.name,str.price);
}
////不可变数组变成可变数组***********************************************
NSLog(@"\n");
NSArray *arr4=@[@"1",@"2",@"3",@"4"];
NSMutableArray *muArr=[NSMutableArray arrayWithArray:arr4];
NSLog(@"%@",muArr);
return0;
}
/////////////////////Student类///////////////////////////
(1)
// Student.h
// OC06_数组
//
// Created by dllo on 15/7/20.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,copy)NSString *name;
-(id)initWithname:(NSString *)name;
@end
(2)
//
// Student.m
// OC06_数组
//
// Created by dllo on 15/7/20.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "Student.h"
@implementation Student
-(id)initWithname:(NSString *)name{
self=[super init];
if (self) {
_name=name;
}
return self;
}
@end
/////////////Book///////////////////////////
(1)///
// Book.h
// OC06_数组
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)CGFloat price;
-(id)initWithName:(NSString *)name
price:(CGFloat)price;
+(id)bookWithName:(NSString *)name
price:(CGFloat)price;
@end
(2)//
// Book.m
// OC06_数组
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "Book.h"
@implementation Book
-(id)initWithName:(NSString *)name
price:(CGFloat)price{
self=[super init];
if (self) {
_name=name;
_price=price;
}
return self;
}
+(id)bookWithName:(NSString *)name
price:(CGFloat)price{
Book *book=[[Book alloc] initWithName:name price:price];
return book;
}
@end
- OC05_数组
- OC05_数组
- OC05_数组省市区
- OC05_内存管理
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- ohasd failed to start错误解决办法
- POJ 2387
- 2015年6月积累
- 12天学好C语言——记录我的C语言学习之路(Day 11)
- 什么叫编译时和运行时
- OC05_数组
- java.lang.IllegalStateException: Failure saving state: active HomeMainFg{42ed2fb8} has cleared index
- 5.编写一个java程序,该程序有两个类:Tank(用于刻画坦克)和Fight(主类)。 已知坦克类如下:
- Ubuntu系统常用操作指令说明
- 那个青春
- 修改Velocity over Lifetime
- 关于如何改变thinkphp中自定义的成功界面、错误界面、异常界面
- HDOJ 5289 Assignment 单调队列
- hdu 5288 OO’s Sequence 2015 Multi-University Training Contest 1