#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;
}