OC05 -- 数组

来源:互联网 发布:南方电网招聘 知乎 编辑:程序博客网 时间:2024/05/09 04:03

//
// main.m
// OC05 — 数组
//
// Created by dllo on 15/7/20.
// Copyright (c) 2015年 Gaozi. All rights reserved.
//

import

import “Student.h”

import “Book.h”

int main(int argc, const char * argv[]) {

//OC数组

//OC中数组存放的一定是对象
// 创建数组的方法:
NSArray *arr = [[NSArray alloc] initWithObjects:@”1”, @”2”, nil];
// 用便利构造器构造一个空数组.
NSArray *arr1 = [NSArray array];
// 用便利构造器构造一个有初始值的数组.
NSArray *arr2 = [NSArray arrayWithArray:arr];
NSLog(@”%@”, arr1);
// 打印结果:1,2;
NSArray *arr3 = [NSArray arrayWithObjects:arr, nil];
NSLog(@”%ld”,arr2.count);
// 打印结果: 1;
// 字面量
NSArray *arr4 = @[@”1”,@”2”,@”3”,@”4”,@”5”];

数组的方法:

// 1. count : 数组里元素个数.

NSArray *arr4 = @[@”1”,@”2”,@”3”,@”4”,@”5”];
NSLog(@”%ld”,arr.count);

// 2. objectAtIndex :通过下标来取值.

NSLog(@"%@",[arr objectAtIndex:0]);NSLog(@"%@",arr[1]);NSLog(@"\n");

// 用FOR循环对数组遍历:

for (NSInteger i = 0; i < arr.count; i++) {
NSLog(@”%@”,arr[i]);
}

// 3. contains - 包含:

NSArray *arr5 = @[@”10”,@”11”,@”12”,@”45”,@”34”,@”21”];
NSLog(@”%d”,[arr containsObject:@”12”]);

// 用For in 对数组中的结构体中的名字遍历:

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];
// NSArray *arr6 = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, nil];

NSArray *arr8 = @[stu1,stu2,stu3];
NSArray *arr9 = @[stu4];
NSArray *arr10 = @[arr8,arr9];

for (Student *stu in arr8) {
NSLog(@”%@”,stu.name);
}

// 对数组中的数组的结构体遍历名字.
for (NSArray *temp in arr10) {
for (Student *stu in temp) {
NSLog(@”%@”,stu.name);
}
}

// 快速枚举:能快速遍历数组等容器对象.

// 针对容器里面对象的遍历.
// 为了增加代码的阅读性,避免不必要的错误,尽量让FOR in 的前部分的类型和数组里的元素类型相同.
NSArray *arr7 = @[@”贱人”,@”傻子”,@”烦人”,@”滚”];
for (NSString *str in arr7) {
NSLog(@”%@”,str);
}

// 对于数组中的数组遍历:

NSArray *arr_1 = @[@”haha”,@”lala”,@”xixi”,@”cici”];
NSArray *arr_2 = @[@”liu”,@”gao”,@”qi”,@”wuwu”];
NSArray *arr_ = @[arr1,arr2];
// 对arr进行遍历.
for (NSArray *temp in arr_) {
for (NSString *str in temp) {
NSLog(@”%@”,str);
}
}

// 可变数组

NSMutableArray *arr = [NSMutableArray array];
// NSMutableArray *arr1 = [[NSMutableArray alloc] init];
NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@”1”,@”2”,@”3”,@”4”,@”5”,@”6”,@”7”,nil];

// 添加一个字符串 – addObject 添加到最后一位.

NSLog(@”添加一个字符串:”);
[arr2 addObject:@”ko”];
NSLog(@”%@”,arr2);
NSLog(@”\n”);

// 移除下标是2 的字符串 – removeObjectAtIndex:

NSLog(@”移除下标是2 的字符串:”);
[arr2 removeObjectAtIndex:2];
//[arr2 removeObjectsInRange:NSMakeRange(2, 1)];
NSLog(@”%@”,arr2);
NSLog(@”\n”);

// 插入一个字符串 – insertObject:

NSLog(@”插入一个字符串:”);
[arr2 insertObject:@”啦啦啦” atIndex:2];
for (NSString *str in arr2) {
NSLog(@”%@”,str);
}
NSLog(@”\n”);

//替换一个对象 – replaceObjectAtIndex:

NSLog(@”替换一个对象:”);
[arr2 replaceObjectAtIndex:6 withObject:@”8”];
for (NSString *str in arr2) {
NSLog(@”%@”,str);
}
NSLog(@”\n”);

//交换两个字符串 – exchangeObjectAtIndex:

NSLog(@”交换两个字符串:”);
[arr2 exchangeObjectAtIndex:3 withObjectAtIndex:5];
for (NSString *str in arr2) {
NSLog(@”%@”,str);
}
NSLog(@”\n”);

// 清空数组 – removeAllObjects:

NSLog(@”清空数组:”);
[arr2 removeAllObjects];
for (NSString *str in arr2) {
NSLog(@”%@”,str);
}
NSLog(@”\n”);

// 练习

//使用可变数组管理所有书籍(定义Book类,包含书名和价格).
Book *book1 = [Book bookWithBookName:@”海的女儿” bookPrice:23.4];
Book *book2 = [Book bookWithBookName:@”童话故事” bookPrice:13.4];
Book *book3 = [Book bookWithBookName:@”芭比的一天” bookPrice:45.6];
Book *book4 = [Book bookWithBookName:@”红与黑” bookPrice:99];
Book *book5 = [Book bookWithBookName:@”小时代” bookPrice:100];
NSMutableArray *arr = [NSMutableArray arrayWithObjects:book1,book2,book3,book4,book5, nil];
// //1. 数组可以添加,删除书籍.
//
// Book *book6 = [Book bookWithBookName:@”爵迹” bookPrice:49.2];
// [arr addObject:book6];
// [arr removeObject:book2];
//
// [arr objectAtIndex:2];
//
// for (Book *book in arr) {
// NSLog(@”%@,%g”,book.bookName,book.bookPrice);
// }
// NSLog(@”\n”);
//
// //2. 可以根据书名查找书籍,并修改价格.
// NSString *str = @”海的女儿”;
// for (Book *book in arr) {
// if ([str isEqualToString:book.bookName]) {
// book.bookPrice = 45;
// // [book changPrice:46];(复杂了!)
// }
// }
// for (Book *book in arr) {
// NSLog(@”%@,%g”,book.bookName,book.bookPrice);
// }
//
// // 3.展示所有书名书单.
// for (Book *temp in arr) {
// NSLog(@”%@”,temp.bookName);
// }
//
// // 4.找到价格是70 - 100 有多少本书,然后把符合条件的书放在一个数组中.
// NSMutableArray *arr2 = [NSMutableArray array];
// for (Book *temp in arr) {
// if (temp.bookPrice <= 100 && 70 <= temp.bookPrice) {
// [arr2 addObject:temp];
// }
// }
// for (Book *temp in arr2) {
// NSLog(@”%@”,temp.bookName);
// }

//5.找到价格是100的书,把书名改成论语.
for (Book *temp in arr) {
if (temp.bookPrice == 100) {
temp.bookName = @”论语”;
}
}
for (Book *temp in arr) {
NSLog(@”%@,%g”,temp.bookName,temp.bookPrice);
}

return 0;

}

0 0