// main.m
// OC_数组整理
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Book.h"
int main(int argc, const char * argv[]) {
// 数组
// OC里的数组
// OC的数组里存放的一定是对象
// NSArray *arr =[[NSArray alloc] init];
// 用便利构造器的方式创建一个空数组
// NSArray *arr =[NSArray array];
// 字面量的方式创建一个数组
// NSArray *arr =@[@"1", @"2", @"3", @"4", @"5"];
// 1.count 数组中元素的个数
// NSLog(@"%ld",arr.count);
// 结果 :2015-07-20 21:00:34.797 OC练习[2167:146055] 5
// 2.objectAtIndex:通过下标进行取值,返回的是一个对象
// NSLog(@"%@",[arr objectAtIndex:1]);
//或者NSLog(@"%@",arr[1]);
// 结果 :2015-07-20 21:03:40.857 OC练习[2175:146925] 2
// for对数组进行遍历
// for (NSInteger i=0; i<arr.count; i++) {
// NSLog(@"%@",arr[i]);
// }
/*结果
2015-07-21 17:30:39.593 OC练习[1830:100110] 1
2015-07-21 17:30:39.594 OC练习[1830:100110] 2
2015-07-21 17:30:39.594 OC练习[1830:100110] 3
2015-07-21 17:30:39.594 OC练习[1830:100110] 4
2015-07-21 17:30:39.594 OC练习[1830:100110] 5
*/
// containsObject:判断元素是否在数组内:BOOL类型
// NSLog(@"%d",[arr containsObject:@"5"]);
// Student *stu1=[[Student alloc] initWithName:@"1"];
// Student *stu2=[[Student alloc] initWithName:@"2"];
// Student *stu3=[[Student alloc] initWithName:@"2" ];
// Student *stu4=[[Student alloc] initWithName:@"4"];
// initWithObjects:建立数组
// NSArray *arr=[[NSArray alloc] initWithObjects:stu1,stu2,stu3,stu4, nil];
// NSArray *arr =@[@"山", @"帅",@"赢",@"林"];
// 快速枚举:能快速的遍历数组等容器对象
// 都是对容器里的对象的遍历
// ()内第一个是数组内元素的类型,第二个是容器
// 为了增加代码的阅读性,避免不必要的错误,尽量让forin的前部分的类型和数组元素类型相同
// for (NSString *str in arr) {
// NSLog(@"%@",str);
// }
/*结果
2015-07-21 19:12:51.815 OC练习[1976:118082] 山
2015-07-21 19:12:51.816 OC练习[1976:118082] 帅
2015-07-21 19:12:51.816 OC练习[1976:118082] 赢
2015-07-21 19:12:51.816 OC练习[1976:118082] 林
*/
// NSArray *arr1=@[@"山",@"帅", @"赢",@"林"];
// NSArray *arr2=@[@"民",@"奇", @"飞", @"姐"];
// NSArray *arr =@[arr1 ,arr2];
// 对arr进行forin遍历
// for (NSArray *temp in arr) {
// for (NSString *str in temp) {
// NSLog(@"%@",str);
// }
// }
/*结果
2015-07-21 19:14:04.972 OC练习[1984:118445] 山
2015-07-21 19:14:04.973 OC练习[1984:118445] 帅
2015-07-21 19:14:04.973 OC练习[1984:118445] 赢
2015-07-21 19:14:04.974 OC练习[1984:118445] 林
2015-07-21 19:14:04.974 OC练习[1984:118445] 民
2015-07-21 19:14:04.974 OC练习[1984:118445] 奇
2015-07-21 19:14:04.974 OC练习[1984:118445] 飞
2015-07-21 19:14:04.974 OC练习[1984:118445] 姐
*/
//1.遍历学生姓名
// 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 *stu in temp) {
// NSLog(@"%@",stu.name);
// }
// }
// 2.遍历学生姓名
// 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,stu4];
// for (Student *temp in arr1) {
// NSLog(@"%@",temp.name);
// }
// initWithArray和initWithObjects:testArr两种定义数组对比
NSArray *testArr =@[@"1", @"2", @"3",@"4"];
// NSArray *arr =[[NSArray alloc] initWithArray:testArr];
// NSArray *arr1 =[[NSArray alloc] initWithObjects:testArr, nil];
// NSLog(@"%@",arr);
// NSLog(@"%@",arr1);
/*结果
2015-07-21 19:22:45.761 OC练习[1998:120868] (1,2,3,4)
2015-07-21 19:22:45.762 OC练习[1998:120868] ((1,2,3,4))
*/
// 便利构造器
// NSArray *arr2 =[NSArray arrayWithArray:testArr];
// NSLog(@"%@",arr2);
// 结果 :2015-07-21 19:27:39.149 OC练习[2014:122551] (1,2,3,4)
// 可变数组
// NSMutableArray *arr =[[NSMutableArray alloc] init];
//
// NSMutableArray *arr1 =[NSMutableArray array ];
// NSMutableArray *arr =[[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3",@"4", nil];
// 1.添加一个字符串
// [arr addObject:@"dska"];
// 添加到数组最后一位
// for (NSString *str in arr) {
// NSLog(@"%@",str);
// }
// 2.移除下标2的字符串
// [arr removeObjectAtIndex:2];
// for (NSString *str in arr) {
// NSLog(@"%@",str);
// }
// 3.插入一个字符串
// [arr insertObject:@"sad" atIndex:2];
// for (NSString *str in arr) {
// NSLog(@"%@",str);
// }
// 4.替换一个字符串
// [arr replaceObjectAtIndex:3 withObject:@"liu "];
// for (NSString *str in arr) {
// NSLog(@"%@",str);
// }
// 5.交换两个字符串
// [arr exchangeObjectAtIndex:0 withObjectAtIndex:1];
// for (NSString *str in arr) {
// NSLog(@"%@",str);
// }
// NSLog(@"%@",arr);
// 6.清空数组
// [arr removeAllObjects];
// NSLog(@"%@",arr);
/*
图书管理
1.使用可变数组管理所有书籍(定义Book类,包含书名和价格);
2.数组可以添加.删除书籍
3.可以从数组根据书名查找书籍,并修改书籍的价格
4.展示所有书籍清单(书名)
*/
// Book *book1 =[Book bookWithName:@"西游记" price:30.5];
// Book *book2 =[Book bookWithName:@"水浒" price:150];
// Book *book3 =[Book bookWithName:@"三国" price:70];
// Book *book4 =[Book bookWithName:@"金瓶梅(插图版)" price:100];
// NSMutableArray *bookArr =[NSMutableArray arrayWithObjects:book1,book2,book3,book4, nil];
//
// 1.对数组进行添加和删除的操作
// Book *book5 =[Book bookWithName:@"红楼梦" price:100];
// [bookArr addObject:book5];
//
// [bookArr removeObject:book3];
// 2.在数组中根据书名查找对应的书,并且对书的价格进行修改
// NSString *str=@"三国";
// for (Book *book in bookArr) {
// if ([book.name isEqualToString:str]) {
// book.price =60;
// NSLog(@"%@, %g",book.name ,book.price);
// }
// }
// 3.展示书的清单
// for (Book *book in bookArr) {
// NSLog(@"书名:%@ 价格:%g",book.name, book.price);
// }
//
// 4.找到价格在70 到100有多少本书,然后把符合条件的书房在一个数组里
// // 数组在使用前一定要初始化
// NSMutableArray *bookArr1=[NSMutableArray array ];
// for (Book *book in bookArr) {
// if ((book.price >=70)&&(book.price<=100)) {
// [bookArr1 addObject:book];
// }
// }
// for (Book *book in bookArr1) {
// NSLog(@"%@",book.name);
// }
//
//
// // 5.找到价格是100的书,把书名改成论语
//
// for (Book *book in bookArr) {
// if (book.price == 100) {
// book.name =@"论语";
// NSLog(@"%@",book.name);
// }
// }
// NSArray *arr = @[@"1", @"2", @"3"];
// NSMutableArray *muArr =[NSMutableArray arrayWithArray:arr];
return 0;
}
// Book.h
// OC_数组整理
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 cml. 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
// Book.m
// OC_数组整理
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 cml. 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
// Student.h
// OC_数组整理
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic ,copy)NSString *name;
-(id)initWithName:(NSString *)name;
+(id)studentWithName:(NSString *)name;
@end
// Student.m
// OC_数组整理
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import "Student.h"
@implementation Student
-(id)initWithName:(NSString *)name{
self =[super init];
if (self) {
_name =name;
}
return self;
}
+(id)studentWithName:(NSString *)name{
Student *student=[[Student alloc] initWithName:name];
return student;
}
@end