数组建立 不可变数组 排序 遍历

//

//  main.m

//  数组

//

//  Created by 博博 on 16/1/7.

//  Copyright (c) 2016年 com.bb. All rights reserved.

//

#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

#import <Foundation/Foundation.h>

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

    @autoreleasepool {

       //创建数组

        //快速创建数组 @[]

        NSArray *week=@[@"mon",@"tue",@"wed",@"thu",@"fir",@"sat",@"sun"];

        //创建空数组

        NSArray *arr=[[NSArray alloc]init];

        NSArray *arr1=[NSArray array];

        //注意数组结尾使用nil(空)进行结束 不可删除

        NSArray *arr2=[NSArray arrayWithObjects:@"apple", @"pear",@"banana",nil];

        NSArray *arr3=[NSArray arrayWithObject:@"b"];

        //复制

        NSArray *newarr=[NSArray arrayWithArray:week];

        NSLog(@"%@",newarr);

        //获取数组中元素个数

        NSInteger count=[newarr count];

        NSLog(@"%ld",count);

        //根据下标访问数组内的元素

        NSString *str=[newarr objectAtIndex:0];

        NSLog(@"%@",str);

        //向数组内添加一个元素

        NSArray *addarr=[newarr arrayByAddingObject:@"ba"];

        NSLog(@"%@",addarr);

        //使用指定的字符串将数组中元素连接起来

        NSString *newstr=[newarr componentsJoinedByString:@","];

        NSLog(@"%@",newstr);

        //判定数组内是否包含某项

        bool result=[newarr containsObject:@"thu"];

        NSLog(@"%d",result);

        //查询某个对象在数组中的位置

        NSInteger index=[newarr indexOfObject:@"thu"];

        NSLog(@"字符串在数组中的位置%d",(int)index+1);

        //获取数组最后一个元素

        NSString *laststring=[newarr lastObject];

        NSLog(@"%@",laststring);

    }

    return 0;

}

//

//  main.m

//  不可变数组

//

//  Created by 博博 on 16/1/7.

//  Copyright (c) 2016年 com.bb. All rights reserved.

//

#import <Foundation/Foundation.h>

#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

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

    @autoreleasepool {

        //可变数组继承不可变数组

        //1创建  设定元素个数进行创建

        NSMutableArray  *mutarr=[NSMutableArray arrayWithCapacity:7];

        //2向数组中添加一个元素

        NSArray *arr=@[@"mon",@"tue",@"wed",@"thu",@"fir",@"sat",@"sun"];

        NSMutableArray *mutarray=[NSMutableArray arrayWithArray:arr];

        [mutarray addObject:@"eig"];

        for(id str in mutarray){

            NSLog(@"%@",mutarray);

        }

        //3根据指定下标插入元素

        [mutarray insertObject:@"nine" atIndex:8];

        //4删除最后一个元素

        [mutarray removeLastObject];

        //5根据指定下标删除元素

        [mutarray removeObjectAtIndex:7];

        //6根据指定对象进行删除

        [mutarray removeObject:@"sun"];

        //7.替换指定下标的元素

        [mutarray replaceObjectAtIndex:0 withObject:@"SUN"];

    }

    return 0;

}

//

//  main.m

//  排序

//

//  Created by 博博 on 16/1/7.

//  Copyright (c) 2016年 com.bb. All rights reserved.

//

#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

#import <Foundation/Foundation.h>

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

    @autoreleasepool {

        //数组内元素排序

        NSArray *arr=@[@"mon",@"tue",@"wed",@"thu",@"fir",@"sat",@"sun"];

        NSArray *sortarr=[arr sortedArrayUsingSelector:@selector(compare:)];

        for (id str in sortarr) {

            NSLog(@"%@",str);

        }

        

        

//        id当不确定数组元素的类型时,可以选择id

//        NSObject和id都是指向任何对象

//        NSObject使用时必须强转

//        

//        instancetype 可以返回和方法所在类相同类型的对象  返回值类型但是绝对不能传参数

//        id只能返回位置类型的对象 参数使用

      

        

        //数组排序--block

        NSComparator sortblock=^(id obj1,id obj2){

            return [obj1 compare(汉字排序lo。。。compare):obj2];

        };

        NSArray *sortarray=[arr sortedArrayUsingComparator:sortblock];

        for(id str in sortarray){

            NSLog(@"%@",str);

        }

        

    }

    return 0;

}

//

//  main.m

//  遍历

//

//  Created by 博博 on 16/1/7.

//  Copyright (c) 2016年 com.bb. All rights reserved.

//

#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

#import <Foundation/Foundation.h>

//数组遍历(枚举)对集合中的元素依次不重复的进行遍历

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

    @autoreleasepool {

        NSArray *arr=@[@"mon",@"tue",@"wed",@"thu",@"fir",@"sat",@"sun"];

        //下标遍历

        for(int i=0;i<[arr count];i++)

        {

            NSString *element=[arr objectAtIndex:i];

            NSLog(@"%@",element);

        }

        for(int i=0;i<[arr count];i++){

            NSLog(@"%@",arr[i]);

        }

        

        //for in 遍历性高

        for (NSString *str in arr) {

            NSLog(@"%@",str);

        }

        //id 当不确定数组元素类型时,选择使用的

        //id nsobject instancettype

        for (id str in arr) {

            NSLog(@"%@",str);

        }

        

        

    }

    return 0;

}

原文地址:https://www.cnblogs.com/bobohahaha/p/5120988.html