//
// main.m
// OC06_数组
//
// Created by dllo on 15/7/20.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Book.h"
int main(int argc, const char * argv[]) {
// int arr[5] = {1, 2, 4, 3, 5};
// for (int i = 0; i < 5 - 1; i++) {
// for (int j = 0; j < 5 - i - 1; j++) {
// if (arr[j] > arr[j + 1]) {
// int temp = 0;
// temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
// }
// }
// }
///************************************************
// OC的数组
// OC的数组里存放的一定是对象
// NSArray *arr = [[NSArray alloc] init];
// 用便利构造器的方式创建一个空数组
// NSArray *arr = [NSArray array];
// 字面量的方式来创建数组
// NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];
// // count: 数组的元素个数
// NSLog(@"%ld", arr.count);
// // 也是通过下标来进行取值,返回的是一个对象
// NSLog(@"%@", [arr objectAtIndex:4]);
// // id 即 void*
// NSLog(@"%@", arr[1]);
//
// // 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 *arr1 = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, stu4, nil];
// 便利构造器的方式
// NSArray *arr2 = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, nil];
//
// for (NSInteger i = 0; i < arr2.count; i++) {
// Student *p = arr2[i];
// NSLog(@"%@", p.name);
// }
// NSLog(@"%@", arr2);
// NSLog(@"%@", arr);
// 快速枚举:能快速的遍历数组等容器对象
// 都是对容器的每一个元素即对象的遍历
// Collection处填要遍历的容器
// type *object最好和元素的类型保持一致
// 为了增加代码的阅读性,避免不必要的错误,尽量让forin的前部分的类型和数组元素类型相同
// NSArray *arr = @[@"薛楠", @"杨林", @"刘珊珊", @"商帅"];
// for (NSArray *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 *arr = @[stu1, stu2, stu3, stu4];
// NSArray *arr1 = @[stu1, stu2, stu3];
// NSArray *arr2 = @[stu4];
// NSArray *arr3 = @[arr1, arr2];
// // 遍历数组里每一个学生的姓名
// for (Student *stu in arr) {
// NSLog(@"%@", stu.name);
// }
// for (NSArray *temp in arr3) {
// for (Student *stu in temp) {
// NSLog(@"%@", stu.name);
// }
// }
// NSArray是不可变的数组
// NSArray *testArr = @[@"1", @"2", @"3"];
// NSArray *arr = [[NSArray alloc] initWithArray:testArr];
// NSArray *arr1 = [[NSArray alloc] initWithObjects:testArr, nil];
// NSLog(@"%@", arr);
// NSLog(@"%ld", arr.count);
// NSLog(@"%@", arr1);
// NSLog(@"%ld", arr1.count);
//
// NSArray *arr2 = [NSArray arrayWithArray:arr];
// 可变数组
// NSMutableArray *arr = [[NSMutableArray alloc] init];
// 空数组
// NSArray *arr = [NSArray array];
// NSMutableArray *arr1 = [NSMutableArray array];
// NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
// // 添加一个字符串
// [arr addObject:@"8"];
// // 添加到数组的最后一位
// NSLog(@"添加一只小猫咪%@", arr);
// // 移除下标2的字符串
// [arr removeObjectAtIndex:2];
// NSLog(@"%@", arr);
// // 插入一个字符串
// [arr insertObject:@"9" atIndex:5];
//
// // 替换一个字符串
// [arr replaceObjectAtIndex:3 withObject:@"10"];
// // 交换两个字符串
// [arr exchangeObjectAtIndex:0 withObjectAtIndex:5];
// // 清空数组
// [arr removeAllObjects];
// NSLog(@"清空数组%@", arr);
// 图书管理。1、使用可变数组管理所有书籍(定义Book类,包含书 名和价格)。2、数组可以添加、删除书籍。 3、可以从数组根据书名查找书籍,并修改书籍的价格 4、展示所有书籍清单(书名)。
Book *book1 = [Book bookWithName:@"红楼梦" price:150];
Book *book2 = [Book bookWithName:@"西游记" price:30.5];
Book *book3 = [Book bookWithName:@"三国" price:80];
Book *book4 = [Book bookWithName:@"水浒传" price:68];
NSMutableArray *bookArr = [NSMutableArray arrayWithObjects:book1, book2, book3, book4, nil];
NSMutableArray *newArr = [NSMutableArray array];
// 1. 对数值进行添加和删除的操作
Book *book5 = [Book bookWithName:@"四书" price:188];
[bookArr addObject:book5];
//Book * temp = [bookArr objectAtIndex:1];
for (Book *temp in bookArr) {
NSLog(@"书名<<%@>>, 价格:%lg", temp.bookname, temp.price);
}
[bookArr removeObject:book1];
NSLog(@"修改\n");
// 2.在数组中根据书名查找对应的书,并且对书的价格进行修改
for (Book *temp in bookArr) {
if ([temp.bookname isEqualToString:@"三国"]) {
temp.price = 1000;
}
}
NSLog(@"展示\n");
// 3.展示书的清单
for (Book *temp in bookArr) {
NSLog(@"书名<<%@>>, 价格:%lg", temp.bookname, temp.price);
}
NSLog(@"查找\n");
// 4.找到价格在70到100有多少本书,然后把符合条件的书放在一个数组里
// 数组在使用之前一定要先创建先初始化
for (Book *temp in bookArr) {
if (temp.price >= 70 && temp.price <= 100) {
NSInteger num = 0;
num++;
[newArr addObject:temp];
NSLog(@"共有:%ld本", num);
}
}
for (Book *temp in newArr) {
NSLog(@"书名<<%@>>, 价格:%lg", temp.bookname, temp.price);
}
NSLog(@"查找修改\n");
// 5.找到价格是100的书,把书名改成论语
for (Book *temp in bookArr) {
if (temp.price == 188) {
temp.bookname = @"论语";
}
}
for (Book *temp in bookArr){
NSLog(@"书名<<%@>>, 价格:%lg",temp.bookname, temp.price);
}
return 0;
}
//
// Book.h
// OC06_数组
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@property(nonatomic, copy)NSString *bookname;
@property(nonatomic, assign)CGFloat price;
// 自定义初始化方法
- (id)initWithBookName:(NSString *)bookName
price:(CGFloat)price;
// 便利构造器
+(id)bookWithName:(NSString *)name
price:(CGFloat)price;
@end
//
// Book.m
// OC06_数组
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Book.h"
@implementation Book
- (id)initWithBookName:(NSString *)bookName
price:(CGFloat)price
{
self = [super init];
if (self) {
_bookname = bookName;
_price = price;
}
return self;
}
// 便利构造器
+(id)bookWithName:(NSString *)name
price:(CGFloat)price
{
Book *book = [[Book alloc] initWithBookName:name price:price];
return book;
}
@end
//
// Student.h
// OC06_数组
//
// Created by dllo on 15/7/20.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic, copy)NSString *name;
- (id)initWithName:(NSString *)name;
@end
//
// Student.m
// OC06_数组
//
// Created by dllo on 15/7/20.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Student.h"
@implementation Student
- (id)initWithName:(NSString *)name
{
self = [super init];
if (self) {
_name = name;
}
return self;
}
@end