iOS前期OC训练OC_06数组

来源:互联网 发布:在淘宝开店怎么取消 编辑:程序博客网 时间:2024/06/01 07:39

//

//  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 = [BookbookWithName:@"红楼梦"price:150];

   Book *book2 = [BookbookWithName:@"西游记"price:30.5];

   Book *book3 = [BookbookWithName:@"三国"price:80];

   Book *book4 = [BookbookWithName:@"水浒传"price:68];

   NSMutableArray *bookArr = [NSMutableArrayarrayWithObjects:book1, book2, book3, book4, nil];

    NSMutableArray *newArr = [NSMutableArrayarray];

    // 1.对数值进行添加和删除的操作

   Book *book5 = [BookbookWithName:@"四书"price:188];

    [bookArraddObject:book5];

    //Book * temp = [bookArr objectAtIndex:1];

   for (Book *tempin bookArr) {

       NSLog(@"书名<<%@>>,价格:%lg", temp.bookname, temp.price);

    }

    [bookArrremoveObject:book1];

   NSLog(@"修改\n");

    // 2.在数组中根据书名查找对应的书,并且对书的价格进行修改

   for (Book *tempin bookArr) {

       if ([temp.booknameisEqualToString:@"三国"]) {

            temp.price =1000;

        }

    }

   NSLog(@"展示\n");

    // 3.展示书的清单

   for (Book *tempin bookArr) {

       NSLog(@"书名<<%@>>,价格:%lg", temp.bookname, temp.price);

    }

   NSLog(@"查找\n");

    // 4.找到价格在70100有多少本书,然后把符合条件的书放在一个数组里

    //数组在使用之前一定要先创建先初始化

   for (Book *tempin bookArr) {

       if (temp.price >=70 && temp.price <=100) {

           NSInteger num = 0;

            num++;

            [newArraddObject:temp];

           NSLog(@"共有:%ld", num);

        }

    }

   for (Book *tempin newArr) {

       NSLog(@"书名<<%@>>,价格:%lg", temp.bookname, temp.price);

    }

 

    NSLog(@"查找修改\n");

    // 5.找到价格是100的书,把书名改成论语

   for (Book *tempin bookArr) {

       if (temp.price ==188) {

            temp.bookname =@"论语";

        }

    }

   for (Book *tempin 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 = [superinit];

   if (self) {

       _bookname = bookName;

       _price = price;

    }

    return self;

}

// 便利构造器

+(id)bookWithName:(NSString *)name

            price:(CGFloat)price

{

   Book *book = [[Bookalloc] initWithBookName:nameprice: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 = [superinit];

   if (self) {

       _name = name;

    }

    return self;

}

@end



0 0