NSArray 演示(无注释)

来源:互联网 发布:ps4网络nat类型失败 编辑:程序博客网 时间:2024/06/14 17:59

#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 = [NSArrayarrayWithObjects:@"etc",@"dev",@"passwd", nil];

    NSString *str = [arraycomponentsJoinedByString:@"/"];

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

}

void arrayTest5()

{

    ArrayTest *a = [[ArrayTestalloc] init];

    a.test = 100;

    ArrayTest *b = [[ArrayTestalloc] init];

    b.test = 100;

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

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

   

    if ([array containsObject:b])

        NSLog(@"%@", array);

    if ([arrayindexOfObject:b] != NSNotFound)

        NSLog(@"%@", b);

}

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

{

    NSString *x = (NSString *)a;

    NSString *y = (NSString *)b;

    return -[xcompare:y options:NSCaseInsensitiveSearch];

}

void arrayTest6()

{

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

    NSArray *res = [strarraysortedArrayUsingFunction:compareFunccontext: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 = [[ArrayTestalloc] init];

    ArrayTest *b = [[ArrayTestalloc] init];

    ArrayTest *c = [[ArrayTestalloc] init];

    a.test = 100;

    b.test = 5;

    c.test = 20;

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

    NSArray *res = [strarraysortedArrayUsingFunction:compareArraycontext:NULL];

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

}

void arrayTest8()

{

    ArrayTest *a = [[ArrayTestalloc] init];

    ArrayTest *b = [[ArrayTestalloc] init];

    ArrayTest *c = [[ArrayTestalloc] init];

    a.test = 100;

    b.test = 5;

    c.test = 20;

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

    NSArray *res = [strarraysortedArrayUsingSelector:

                    @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:jwithObjectAtIndex:j+1];

        }

    }

}

void arrayTest9()

{

    ArrayTest *a = [[ArrayTestalloc] init];

    ArrayTest *b = [[ArrayTestalloc] init];

    ArrayTest *c = [[ArrayTestalloc] init];

    a.test = 100;

    b.test = 5;

    c.test = 20;

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

    bubble(strarray);

    NSLog(@"%@", strarray);

}

void arrayTest10()

{

    NSMutableArray *array = [NSMutableArrayarrayWithCapacity: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;

}

0 0
原创粉丝点击