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;
}
- NSArray 演示(无注释)
- NSString 演示(无注释)
- Ogre水波演示Demo(注释)
- Ogre水波演示Demo(注释)
- USB挂载小程序(无注释)
- 演示无驱动执行Ring0代码(C++) (转)
- 无bug注释
- java Properties存取操作例子(无注释)
- 双向链表(无注释...慎入)
- js---javaScript 1、标识符、注释、变量、基本数据类型、运算符(演示实例)
- NSArray使用(五)
- NSArray数组(1)
- NSArray
- NSArray
- NSArray
- NSArray
- NSArray
- NSArray
- 含有图片上传功能的请求及数据库的存储
- Android Launcher分析和修改12——Widget列表信息收集
- Dijkstra算法(单源最短路径)
- 在同一个地方生活
- Java基础视频教程-概述
- NSArray 演示(无注释)
- 什么是白箱测试、黑箱测试、回归测试?
- 链表反转
- perl socket编程
- unity3d XML配置表读写
- SVN更改登录用户
- C++ 类型转换
- iOS多线程和runLoop介绍
- C# 调用PrintDocument对象进行打印和测试