IOS-OC-数组和字典、数组的选择法和冒泡法

一:【数组】

【注】OC兼容C的数组,C的数组用于存储基础数据类型(int, char, float)数据和复合数据类型 (int *, int[10])数据;使用OC的数组对象存储类的对象。

【注】NSMutableArray : NSArray
1.NSArray的方法NSMutableArray都可以用
2.传参需要传入NSArray * 也可以传入NSMutableArray *

一.不可变数组NSArray
【注】不可变数组,指数组对象一旦创建,其元素的个数和顺序不可修改。

NSArray * array = @[@"One One", @"Two", @"Three", @"Four", @"dDD"];

//数组本身是一个对象
//数组的元素如@"One" @"Two" @"Three"等都是任意类的对象,不仅限于字符串。
//创建数组对象时传参,传入对象的地址
//数组中只是存储了对象的地址,而非存储了对象的本体。

//同一个对象可以存储到两个数组当中,仍然是一个对象
//在一个数组中修改了对象,在另一个数组中读取对象,会发现对象也被修改了。

//遍历打印一个数组
//打印一个数组,就是打印数组的元素
NSLog(@"%@", array);

//返回array数组的元素个数
NSUInteger count = [array count];
NSLog(@"%lu", count);

NSArray * array = @[@"One One", @"Two", @"Three", @"Four", dog];

【方法】
1.根据索引,返回数组的元素

Dog * dog2 = [array1 objectAtIndex:0];
Dog * dog3 = array2[0];
//这个种方法是等价的,返回第0个元素的地址,因为元素是Dog对象,所以用指向这种对象的指针来接收

2.返回数组的元素个数

NSUInteger count = [array count];

3.字符串的分割 strtok

NSArray * subStrings = [str componentsSeparatedByString:@" "];
NSArray * subStrings = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", "]];

4.数组中字符串的拼接

NSString * _str = [subStrings componentsJoinedByString:@"*"];

5.数组的遍历 快速枚举法
//通过索引遍历

for (int i = 0; i < [array count]; i++) {
    NSLog(@"%@", array[i]);
}

//快速枚举法

for (NSString * str in array) {
    //第一次循环str指向数组第一个元素,第二次循环,str指向数组第二个元素。。。
    NSLog(@"%@", str);
}

【注】在for in中break和continue都能使用
【注】如果遍历的是可变数组,在for in结束前,不能修改可变数组的元素个数和顺序。

二.可变数组NSMutableArray
1.重置可变数组

[array setArray:@[@"One", @"Two", @"Three", @"Four"]];

2.增加元素
追加:(添加元素)

[array addObject:@"Five"];

插入:

[array insertObject:@"Six" atIndex:2];

3.删除元素
//删除元素
//删除指定元素,传入需要删除的元素的地址,删除这个元素
//如果要删的是字符串,只需传入和所删字符串相等的字符串就可以了,不用传入同一个字符串。
//如果数组中有多个@"Four"会一起删除

[array removeObject:@"Four"];

//删除指定索引的元素

[array removeObjectAtIndex:2];

4.交换两个元素的位置

[array exchangeObjectAtIndex:0 withObjectAtIndex:2];

二:【字典】

【注】NSMutableDictionary : NSDictionary

一.字典

NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"One", @"1", @"Two", @"2", @"Three", @"3", nil];
NSDictionary * dict2 = @{@"1": @"One", @"2": @"Two", @"3": @"Three"};

1.返回键值对个数

NSUInteger count = [dict2 count];

2.通过键返回值

NSString * str = [dict2 objectForKey:@"2"];

3.返回所有的键或值
//返回所有的键

NSArray * keys = [dict2 allKeys];
//返回所有的值
NSArray * values = [dict2 allValues];

4.快速枚举法遍历
//快速枚举法遍历

for (NSString * key in dict2) {
    //快速枚举法只能遍历字典的键
    NSLog(@"%@", [dict2 objectForKey:key]);
    //通过key再去查找值
}

二.可变字典
1.重置字典

[dict setDictionary:dict];

2.添加键值对

[dict setObject:@"Four" forKey:@"0"];

3.删除键值对

[dict removeObjectForKey:@"0"];

字典实操

//1.创建学生类,成员变量有姓名,年龄和成绩
//用学生类,创建三个学生对象,将三个学生添加到字典中,以学生的名字作为key。
//输入一个学生的名字,打印出该学生的完整信息。
//学生对象是值,学生名字的字符串是key
#import 
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 1. 获取对象
        Student *studentOne = [[Student alloc] initWithName:@"EZ" age:21 score:23.1];
        Student *studentTwo = [[Student alloc] initWithName:@"VN" age:10 score:34.5];
        Student *studentThree = [[Student alloc] initWithName:@"TM" age:23 score:56.9];
        
                // 2. 存储对象
        NSDictionary *dict = @{
                               @"EZ":studentOne,
                               @"VN":studentTwo,
                               @"TM":studentThree
                               };
        
        //3. 获取学生的姓名
        char name[20];
        scanf("%s",name);
        NSString *nameOfStudent = [NSString stringWithFormat:@"%s",name];
        
        //4.从字典中取出学生对象
        Student *result = dict [nameOfStudent];
        if (result) {
            [result printfDetailOfStudent];
        }
        
    }
    return 0;
}
#import 

@interface Student : NSObject
{
    NSString *_name;
    int _age;
    float _score;
}

- (instancetype)initWithName:(NSString *)name age:(int)age score:(float )score;

- (void)printfDetailOfStudent;
@end
#import "Student.h"

@implementation Student
- (instancetype)initWithName:(NSString *)name age:(int)age score:(float)score
{
    if (self = [super init]) {
        _name = name;
        _age = age;
        _score = score;
    }
    return self;
}

- (void)printfDetailOfStudent
{
    NSLog(@"学生的名字是:%@ 年龄是:%d 生物成绩:%f",_name,_age,_score);
}
@end


练习:
1.已知有数组@[@“One”, @“Two”, @“Three”, @“Four”];
编写程序,遍历打印数组的每个元素。

*2.创建一个可变字符串,将字符串中每个字符,逆序

- (NSMutableString *)reverseMutableString:(NSMutableString *)str;
//传入@“hello”
//变成@“olleh”

字典初认识

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool { 
        // 成对出现(键值对)
        // 数据没有顺序
        // key  value
        // 实例化字典的方法一
        NSDictionary *dict = @{@"1":@"呵呵",@"2":@"哈哈",@"3":@"嘿嘿"};
        // 实例化字典的方法二
        NSDictionary *dictTwo = [[NSDictionary alloc] initWithObjectsAndKeys:@"呵呵",@"1",@"哈哈",@"2",@"嘿嘿",@"3", nil];
        
        // 从字典中取值
        NSString *str = dict[@"1"];
        NSString *strTwo = [dict objectForKey:@"2"];
        
        // 1. 返回键值对的个数
        NSInteger count = [dict count];
        NSInteger countT = dict.count;
        
        // 2. 返回所有的键
        NSArray *arrayKeys = [dict allKeys];
        
        NSLog(@"%@",arrayKeys);
        // 3. 返回所有的值
     //   NSArray *arrayValues = [dict allValues];
      //  NSLog(@"%@",arrayValues);
        // 遍历字典
        // 快速枚举法 只能遍历字典的键
     /*   for (NSString *key in dict) {
            NSLog(@"----%@",key);
            NSLog(@"%@",dict[key]);
        }
       */
        // 可变字典
     /*
        NSMutableDictionary *dictM =
        [[NSMutableDictionary alloc] init];
        // 重置
        [dictM  setDictionary:dict];
        NSLog(@"%@",dictM);
        
        // 2.添加键值对
     [dictM setObject:@"20000000" forKey:@"you"];
        NSLog(@"%@",dictM);
        
        // 3.删除键值对
   // [dictM removeObjectForKey:@"3"];
      */
    }
      
    return 0;
}

作业:

【数组】
1.创建一个数组,元素是@"One", @"Two", @"Three", @"Four",使用三种方法 遍历打印数组的元素。
NSLog(@"%@",array);

2.创建一个数组,元素是 5 个 Dog 类的对象地址,遍历数组,使每个元素执行 bark 函数。

3.创建一个数组,元素是两个字符串对象地址和三个Dog类的对象地址,遍历数组,如果是字符串那么输出"I am a string!"如果是 Dog 类的对象那么执行 bark 方法。

4.设计⼀个学生类,成员有姓名,年龄,学号,成绩。 要求,以上述每⼀成员,分别进行排序(学号按升序,成绩按降序,年龄升序,名字降序)。
5.@“Yes I am a so bad man”,按照单词逆序

6.有两个字符串@"This is a boy";@"really fucking bad"; 将这两个字符串单词,交叠,形成系的字符串 @"This really is fucking a bad boy"。

【字典】
1.创建学生类,成员变量有姓名,年龄和成绩
用学生类,创建三个学生对象,将三个学生添加到字典中,以学生的名字作为key。
输入一个学生的名字,打印出该学生的完整信息。
//学生对象是值,学生名字的字符串是key

【自学】

NSArray:

- (NSArray *)arrayByAddingObject:(id)anObject;
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;

- (NSUInteger)indexOfObject:(id)anObject;
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;
NSMutableArray:

- (void)addObjectsFromArray:(NSArray *)otherArray;
- (void)removeObjectsInArray:(NSArray *)otherArray;
- (void)removeObjectsInRange:(NSRange)range;

- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;

//- (void)sortUsingComparator:(NSComparator)cmptr;

五.数组的排序

【选择法】

5 4 3 2 1
4 5 3 2 1

1 5 4 3 2
1 4 5 3 2
1 3 5 4 2
1 2 5 4 3



// 第一步
if (a[0]>a[1]) {
    int tmp = a[0];
    a[0] = a[1];
    a[1] = tmp;
}

// 第二步
for (int i = 1; i < 5; i ++ ) {
    if (a[0]>a[i]) {
        int tmp = a[0];
        a[0] = a[i];
        a[i] = tmp;
    }
}


// 第三步
int a[5] = {5,4,3,2,1};
for (int i = 0; i < 5 - 1; i ++) { // 用第几个数和以后的对比
    
    for (int j = i +1; j < 5; j ++) {
        
        if (a[i] > a[j]) {
            int tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }
    }
}
// 打印
for (int i = 0; i < 5 ;i ++) {
    printf("%d",a[i]);
}
printf("\n");

if 1

NSLog(@"dd");

else

NSLog(@"ss");

endif

【冒泡法】

5 4 3 2 1
4 5 3 2 1
4 3 5 2 1

// 第一步
if (a[0] > a[1]) {
int tmp = a[0];
a[0] = a[1];
a[1] = tmp;
}

// 第二步
for (int i = 0; i < 4; i ++) {
if (a[i] > a[i +1]) {
int tmp = a[i];
a[i] = a[i +1];
a[i +1] = tmp;
}
}

// 第三步
int a[5] = {5,4,3,2,1};
for (int i = 0; i < 5 - 1; i ++) {
for (int j = 0; j < 5 - i - 1; j ++) {
if (a[j] > a[j + 1]) {
int tmp = a[j];
a[j] = a[j + 1 ];
a[j+1] = tmp;
}
}
}

for (int i = 0; i < 5 ;i ++) {
printf("%d",a[i]);
}
printf("\n");

你可能感兴趣的:(IOS-OC-数组和字典、数组的选择法和冒泡法)