首页 > 代码库 > OC 第七天 容器类 lesson7

OC 第七天 容器类 lesson7

NSArray 常用方法

1.创建数组对象,使用初始化方法或便利构造器。

<span style="font-size:18px;">        NSArray *arr1 = [[NSArray alloc]initWithObjects:@"王晨",@"刘国伟",@"郑海坤",nil];
        NSArray *arr2 = [NSArray arrayWithObjects:@"李智",@"魏大卫",@"刘天伟", nil];
        NSArray *arr3 = [NSArray arrayWithArray:arr1];
        NSLog(@"%@\n%@\n%@\n", arr1, arr2, arr3);</span>

运行结果:

2014-09-19 19:29:06.750 oc07_容器类[5676:303] (

    "\U738b\U6668",

    "\U5218\U56fd\U4f1f",

    "\U90d1\U6d77\U5764"

)

(

    "\U674e\U667a",

    "\U9b4f\U5927\U536b",

    "\U5218\U5929\U4f1f"

)

(

    "\U738b\U6668",

    "\U5218\U56fd\U4f1f",

    "\U90d1\U6d77\U5764"

)


2.获取元素个数。

<span style="font-size:18px;"><span style="white-space:pre">	</span>for (int i = 0; i < [arr1 count]; i++) {</span>

3.根据索引值获取对象。

<span style="font-size:18px;">        NSLog(@"%@",[arr1 objectAtIndex:i]);
        NSLog(@"%@", arr1[i]);
        }</span>
运行结果:

2014-09-19 19:29:06.752 oc07_容器类[5676:303]王晨

2014-09-19 19:29:06.752 oc07_容器类[5676:303]王晨

2014-09-19 19:29:06.752 oc07_容器类[5676:303]刘国伟

2014-09-19 19:29:06.753 oc07_容器类[5676:303]刘国伟

2014-09-19 19:29:06.753 oc07_容器类[5676:303]郑海坤

2014-09-19 19:29:06.754 oc07_容器类[5676:303]郑海坤

4.获取某对象在数组中的索引值。

<span style="font-size:18px;"><span style="white-space:pre">	</span>NSLog(@"%lu",[arr1 indexOfObject:@"王晨"]);</span>
运行结果:

2014-09-19 19:29:06.754 oc07_容器类[5676:303] 0



NSMutableArray 常用方法

 1.创建数组对象。

<span style="font-size:18px;"><span style="white-space:pre">	</span>NSMutableArray * mutArry1 = [NSMutableArray arrayWithArray:arr1];</span>
2.添加元素、插入元素。
<span style="font-size:18px;">        [mutArry1 addObject:@"陈佳鸿"];//添加
        [mutArry1 addObjectsFromArray:arr2];
       
        NSString *temp = @"woca";
        [mutArry1 insertObject:temp atIndex:4];//插入
        for (int i = 0; i < [mutArry1 count]; i++) {
            NSLog(@"%@",mutArry1[i]);
        }</span>
运行结果:

2014-09-19 20:23:31.004 oc07_容器类[6755:303]王晨

2014-09-19 20:23:31.004 oc07_容器类[6755:303]刘国伟

2014-09-19 20:23:31.005 oc07_容器类[6755:303]郑海坤

2014-09-19 20:23:31.005 oc07_容器类[6755:303]陈佳鸿

2014-09-19 20:23:31.005 oc07_容器类[6755:303] woca

2014-09-19 20:23:31.006 oc07_容器类[6755:303]李智

2014-09-19 20:23:31.006 oc07_容器类[6755:303]魏大卫

2014-09-19 20:23:31.007 oc07_容器类[6755:303]刘天伟

数组的嵌套遍历:

<span style="font-size:18px;">        NSMutableArray *class19 = [NSMutableArray arrayWithObjects:arr1, arr2, arr3 ,nil];
        
        //方法1
        for (int i = 0; i < [class19 count]; i++) {
            for (int j = 9; j < [class19[i] count]; j++) {
                NSLog(@"%@",class19[i][j]);
            }
        }
        //方法2
        for (int i = 0; i < [class19 count]; i++) {
            for (int j = 0; j < [[class19 objectAtIndex:i] count]; j++) {
                NSLog(@"%@",[[class19 objectAtIndex:i]objectAtIndex:j]);
            }
        }
        //方法3
        for (int i = 0; i < [class19 count]; i++) {
            NSArray *arr = [class19 objectAtIndex:i];
            for (int j = 0; j < [arr count]; j++) {
                NSLog(@"%@",arr[j]);
            }
        }</span>

3.删除元素、替换元素。

<span style="font-size:18px;">        [mutArry1 removeLastObject];
        [mutArry1 removeObject:@"刘天伟"];
        //        替换
        [mutArry1 replaceObjectAtIndex:5 withObject:@"王子洁"];
        for (int i = 0; i < [mutArry1 count]; i++) {
            NSLog(@"%@",mutArry1[i]);
        }</span>


运行结果:

2014-09-19 20:28:01.038 oc07_容器类[6782:303]王晨

2014-09-19 20:28:01.038 oc07_容器类[6782:303]刘国伟

2014-09-19 20:28:01.039 oc07_容器类[6782:303]郑海坤

2014-09-19 20:28:01.039 oc07_容器类[6782:303]陈佳鸿

2014-09-19 20:28:01.039 oc07_容器类[6782:303] woca

2014-09-19 20:28:01.040 oc07_容器类[6782:303]王子洁

2014-09-19 20:28:01.040 oc07_容器类[6782:303]魏大卫

4.交换指定位置的两个元素。

<span style="font-size:18px;">        [mutArry1 exchangeObjectAtIndex:4 withObjectAtIndex:5];
        for (int i = 0; i < [mutArry1 count]; i++) {
            NSLog(@"%@",mutArry1[i]);
        }</span>

关于排序:

<span style="font-size:18px;">NSMutableArray *aryNum = [NSMutableArray arrayWithObjects:@"3", @"7", @"9", @"1", @"5", nil];</span>

冒泡方法:

<span style="font-size:18px;">        int flag = 1;
        for (int i = 0; i < [aryNum count ] - 1 && 1 == flag; i++) {
            flag = 0;
            for (int j = 0; j < [aryNum count] - i - 1; j++) {
                if ([aryNum[j] compare: aryNum[j + 1]] == 1) {
                    [aryNum exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
                    flag = 1;
                }
            }
        }
        for (int i = 0; i < [aryNum count]; i++) {
            NSLog(@"%@",aryNum[i]);
        }
</span>
<span style="font-size:18px;"> 1.可变数组的排序方法
        [aryNum sortUsingSelector:@selector(compare:)];
2.不可变数组的排序方法
        aryNum = (NSArray *)[aryNum sortedArrayUsingSelector:@selector(compare:)];</span>



NSNumber 常用方法

<span style="font-size:18px;">//1.创建各个类型得数值对象
        //(1)整型
        NSNumber *n1 = [NSNumber numberWithInt:43];
        //(2)浮点(单精度)
        NSNumber *n2 = [NSNumber numberWithFloat:3.14];
        //(3)浮点(双精度)
        NSNumber *n3 = [NSNumber numberWithDouble:65.1131313123123123123];
        //(4)长整型
        NSNumber *n4 = [NSNumber numberWithLong:987654321];
        //(5)字符型
        NSNumber *n5 = [NSNumber numberWithChar:'A'];</span>


NSMutableArray 常用方法

<span style="font-size:18px;">        NSMutableArray *arrNum = [NSMutableArray arrayWithObjects:n1, n2, n3, n4, n5, nil];
        NSLog(@"%@",arrNum);</span>
运行结果:

2014-09-19 20:28:01.044 oc07_容器类[6782:303] (

    43,

    "3.14",

    "65.11313131231232",

    987654321,

    65

)

对arrNum排序:

<span style="font-size:18px;">        [arrNum sortUsingSelector:@selector(compare:)];
        NSLog(@"%@",arrNum);</span>
运行结果:

2014-09-19 20:28:01.045 oc07_容器类[6782:303] (

    "3.14",

    43,

    65,

    "65.11313131231232",

    987654321

)


遍历arrNum

        for (int i = 0; i < [arrNum count]; i++) {
            double d = [arrNum[i] doubleValue];
            NSLog(@"%.9f",d);
        }


运行结果:

2014-09-19 20:28:01.045 oc07_容器类[6782:303] 3.140000105

2014-09-19 20:28:01.045 oc07_容器类[6782:303] 43.000000000

2014-09-19 20:28:01.046 oc07_容器类[6782:303] 65.000000000

2014-09-19 20:28:01.046 oc07_容器类[6782:303] 65.113131312

2014-09-19 20:28:01.047 oc07_容器类[6782:303] 987654321.000000000







OC 第七天 容器类 lesson7