NSArray 演示(无注释)

本文探讨了Objective-C中使用NSLog打印数组信息、数组元素共享特性、数组元素属性修改及其影响、数组元素遍历、数组字符串连接、数组元素比较、数组排序、冒泡排序算法应用、数组元素删除等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <Foundation/Foundation.h>

#import "ArrayTest.h"

void arrayTest1()

{

    NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2", @"abc", nil];

    NSLog(@"%@, %d, %@", array, (int)array.count,

          [array objectAtIndex:0]);

}

void arrayTest2()

{

    //数组之间元素是否共享

    ArrayTest *x = [[ArrayTest alloc] init];

    x.test = 999;

    id carray[3] = {@"1", @"2", x};

    NSArray *array = [NSArray arrayWithObjects:carray count:3];

    NSArray *array2 = [NSArray arrayWithArray:array];

    ArrayTest *x0 = [array objectAtIndex:2];

    ArrayTest *x1 = [array2 objectAtIndex:2];

    x0.test = 100;

    NSLog(@"%@(%d), %@(%d)", array, x0.test,

          array2, x1.test);

}

void arrayTest3()

{

    NSArray *array = [NSArray arrayWithObjects:@"1", @"2",[NSNumber numberWithInt:10], nil];

    for (int i=0; i < array.count; i++) {

        id obj = [array objectAtIndex:i];

        NSLog(@"%@", obj);

    }

    // for (type in 数组)

    for (id a in array) {

        NSLog(@"%@", a);

    }

}

void arrayTest4()

{

    NSArray *array = [NSArray arrayWithObjects:@"etc",@"dev", @"passwd", nil];

    NSString *str = [array componentsJoinedByString:@"/"];

    NSLog(@"%@\n%@", [str componentsSeparatedByString:@"/"], str);

}

void arrayTest5()

{

    ArrayTest *a = [[ArrayTest alloc] init];

    a.test = 100;

    ArrayTest *b = [[ArrayTest alloc] init];

    b.test = 100;

    NSString *str = [NSString stringWithFormat:@"%s", "123"];

    NSArray *array = [NSArray arrayWithObjects:a, @"123",@"456", nil];

   

    if ([array containsObject:b])

        NSLog(@"%@", array);

    if ([array indexOfObject:b] != NSNotFound)

        NSLog(@"%@", b);

}

NSInteger compareFunc(id a, id b, void *context)

{

    NSString *x = (NSString *)a;

    NSString *y = (NSString *)b;

    return -[x compare:y options:NSCaseInsensitiveSearch];

}

void arrayTest6()

{

    NSArray *strarray = [NSArray arrayWithObjects:@"c",@"a", @"b", nil];

    NSArray *res = [strarray sortedArrayUsingFunction:compareFunc context:NULL];

    res = [strarray sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"%@\n%@", strarray, res);

}

NSInteger compareArray(id a, id b, void *context)

{

    ArrayTest *x = (ArrayTest *)a;

    ArrayTest *y = (ArrayTest *)b;

    if (x.test > y.test)

        return 1;

    else if (x.test < y.test)

        return -1;

    else

        return 0;

}

void arrayTest7()

{

    ArrayTest *a = [[ArrayTest alloc] init];

    ArrayTest *b = [[ArrayTest alloc] init];

    ArrayTest *c = [[ArrayTest alloc] init];

    a.test = 100;

    b.test = 5;

    c.test = 20;

    NSArray *strarray = [NSArray arrayWithObjects:a,b,c, nil];

    NSArray *res = [strarray sortedArrayUsingFunction:compareArray context:NULL];

    NSLog(@"%@\n%@", strarray, res);

}

void arrayTest8()

{

    ArrayTest *a = [[ArrayTest alloc] init];

    ArrayTest *b = [[ArrayTest alloc] init];

    ArrayTest *c = [[ArrayTest alloc] init];

    a.test = 100;

    b.test = 5;

    c.test = 20;

    NSArray *strarray = [NSArray arrayWithObjects:a,b,c, nil];

    NSArray *res = [strarray sortedArrayUsingSelector:

                    @selector(compareA:)];

    

    NSLog(@"%@\n%@", strarray, res);

}

void bubble(NSMutableArray *array)

{

    int cnt = (int)array.count;

    int i, j;

    ArrayTest *x, *y;

    for (i = 1; i < cnt; i++) {

        for (j = 0; j < cnt-i; j++) {

            x = (ArrayTest *)[array objectAtIndex:j];

            y = (ArrayTest *)[array objectAtIndex:j+1];

            if ([x compareA:y] == 1)

                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];

        }

    }

}

void arrayTest9()

{

    ArrayTest *a = [[ArrayTest alloc] init];

    ArrayTest *b = [[ArrayTest alloc] init];

    ArrayTest *c = [[ArrayTest alloc] init];

    a.test = 100;

    b.test = 5;

    c.test = 20;

    NSMutableArray *strarray = [NSMutableArray arrayWithObjects:a,b,c, nil];

    bubble(strarray);

    NSLog(@"%@", strarray);

}

void arrayTest10()

{

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

    [array addObject:@"1"];

    [array addObject:@"2"];

    [array addObject:@"3"];

//    for (NSString * s in array) {

//        if ([ s isEqualToString:@"1"]) {

//            [array removeObject:s];

//            //break;

//        }

//    }

    for (int i = 0; i < array.count; i++) {

        NSString *s = [array objectAtIndex:i];

        if ([s isEqualToString:@"1"])

            [array removeObject:s];

    }

}

/*

    SEL x = @selector(方法名) = @selector(initWithCapacity:);

 */

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

{


    @autoreleasepool {

        

        arrayTest2();

        

    }

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值