@fiy-fish
2015-07-15T20:08:37.000000Z
字数 5261
阅读 1372
Objective-c
// main.m
// day04-04-NSArray2
//
// Created by Aaron on 15/7/6.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
/*
NSArray *array = @[@1,@2,@3];
[array count];
[array objectAtIndex:0];
id obj = array[0];
[array indexOfObject:@1];
[array containsObject:@1];
[array firstObject];
[array lastObject];
[array indexOfObject:@1 inRange:NSMakeRange(0, 2)];
[array subarrayWithRange:NSMakeRange(0, 2)];
[array objectsAtIndexes:[NSIndexSet indexSetWithIndex:2]];
*/
//数组的遍历
#if 0
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *array = @[@"hello",@"world",@"hello",@"nihao"];
//第一种方式:循环
#if 0
for(int i = 0; i < array.count; i++)
{
id obj = array[i];
NSLog(@"%@",obj);
}
for(NSInteger i = [array count]-1; i >= 0; i--)
{
id obj = array[i];
NSLog(@"%@",obj);
}
#endif
//第二种方式: for in 结构
//快速遍历
//不能方便的使用i做下标
//在使用for in结构的时候,不要改变数组的容量
//只能完整的正序遍历一次
//for(id obj in array)
for(NSString *obj in array)
{
// if([obj isKindOfClass:[NSNumber class]])
// {
//NSLog(@"%@ %ld",obj,[array indexOfObject:obj]);
// }
}
//第三种方式:枚举器
NSEnumerator *enumerator = [array objectEnumerator];
enumerator = [array reverseObjectEnumerator];
id obj = nil;
while(obj = [enumerator nextObject])
{
NSLog(@"%@",obj);
}
}
return 0;
}
#endif
#if 0
int main()
{
NSArray *array = @[@90,@88,@99,@50,@76,@52];
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for(int i = 0; i < [array count]; i++)
{
NSNumber *obj = [array objectAtIndex:i];
if([obj integerValue] >= 60)
{
[indexSet addIndex:i];
}
}
NSArray *sub = [array objectsAtIndexes:indexSet];
NSLog(@"%@",sub);
}
#endif
//字符串和数组
#if 0
int main()
{
NSString *path = @"/users/Aaron/desktop/file.txt";
//以指定的字符串来切割字符串
NSArray *array = [path componentsSeparatedByString:@"/d"];
array = [path componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
NSLog(@"%@",array);
NSMutableString *str = [NSMutableString string];
for(id obj in array)
{
if(![obj isEqualToString:@""])
{
[str appendFormat:@"/%@",obj];
//NSLog(@"%@",obj);
}
}
NSLog(@"%@",str);
NSString *newStr = [array componentsJoinedByString:@"/"];
NSLog(@"%@",newStr);
//http://www.taobao.com/market/nvzhuang/qiang.php?spm=a21bo.7724922.8458.2.3nzdsq&acm=20140506001.1003.2.331846&uuid=AEDSuvcX_6UEJDkEXPAQCAXE5qpJkVjym&scm=1003.2.20140506001.OTHER_1436484331037_331846&pos=1
NSArray *array1 = @[@"name=xiaobai",@"number=100",@"device=Iphone"];
NSString *sss = [array1 componentsJoinedByString:@"&"];
NSLog(@"%@",sss);
}
#endif
//可变数组
#if 0
int main()
{
NSMutableArray *array = [NSMutableArray array];
NSMutableArray *array1 = [NSMutableArray arrayWithArray:@[@1,@2]];
//可变数组仅仅是在不可变的基础上添加了增删改
//增
//addObject: 往数组中增加一个元素,注意是增加到末尾
[array1 addObject:@3];
NSLog(@"%@",array1);
//插入元素到指定的索引
[array1 insertObject:@"..." atIndex:0];
NSLog(@"%@",array1);
//将其他数组的元素按顺序添加到当前数组
[array addObjectsFromArray:array1];
NSLog(@"-----%@",array);
// NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
// [indexSet addIndex:0];
// [indexSet addIndex:2];
// [indexSet addIndex:1];
// [indexSet addIndex:3];
// [array insertObjects:array1 atIndexes:indexSet];
// NSLog(@"%@",array);
//删除数组中的某一个对象
//只要出现的对象都删掉
[array addObject:@"..."];
//[array removeObject:@"..."];
NSLog(@"%@",array);
//删掉指定的索引对应的对象
[array removeObjectAtIndex:0];
NSLog(@"%@",array);
//删除所有对象
//[array removeAllObjects];
//替换元素
[array replaceObjectAtIndex:3 withObject:@"+++++"];
NSLog(@"%@",array);
//快速修改数组
[array setArray:@[@0,@9,@8]];
NSLog(@"%@",array);
}
#endif
#if 0
int main()
{
NSMutableArray *array = [NSMutableArray array];
for(int i = 0; i < 5; i++)
{
int temp;
scanf("%d",&temp);
[array addObject:[NSNumber numberWithInt:temp]];
}
NSLog(@"%@",array);
}
#endif
#if 0
int main()
{
//数组排序
NSMutableArray *array = [NSMutableArray array];
for(int i = 0 ; i < 20; i++)
{
NSString *str = [NSString stringWithFormat:@"string%d",i];
[array addObject:str];
}
//第一种方式: 自己用循环来实现
for(int i = 0; i < [array count]-1; i++)
{
for(int j = 0; j < [array count]-i-1; j++)
{
if([(NSString *)[array objectAtIndex:j] compare:[array objectAtIndex:j+1]] == NSOrderedAscending)
{
[array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSLog(@"%@",array);
}
#endif
#import "Dog.h"
//#if 0
int main()
{
NSMutableArray *array = [NSMutableArray array];
for(int i = 0; i < 10; i++)
{
Dog *dog = [[Dog alloc] initWithAge:arc4random_uniform(20) andWeight:arc4random_uniform(50)];
[array addObject:dog];
}
NSLog(@"%@",array);
//#endif
//#if 0
for(int i = 0; i < [array count]-1; i++)
{
for(int j = 0; j < [array count]-i-1; j++)
{
if([(Dog *)[array objectAtIndex:j] compareByAgeWithOtherDog:[array objectAtIndex:j+1]] == NSOrderedAscending)
// if([[array objectAtIndex:j] age] < [[array objectAtIndex:j+1] age])
{
[array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
//#endif
//#if 0
[array sortUsingSelector:@selector(sortByAgeWithOtherDog:)];
NSLog(@"%@",array);
}
//#endif
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
NSInteger _age;
NSInteger _weight;
}
-(instancetype)initWithAge:(NSInteger)age andWeight:(NSInteger)weight;
-(NSComparisonResult)compareByAgeWithOtherDog:(Dog *)dog;
-(BOOL)sortByAgeWithOtherDog:(Dog *)dog;
-(NSInteger)age;
//-(NSInteger)weight;
@end
#import "Dog.h"
@implementation Dog
-(instancetype)initWithAge:(NSInteger)age andWeight:(NSInteger)weight
{
if(self = [super init])
{
_age = age;
_weight = weight;
}
return self;
}
-(NSInteger)age
{
return _age;
}
//-(NSInteger)weight{
// return _weight;
//}
-(NSComparisonResult)compareByAgeWithOtherDog:(Dog *)dog
{
if(_age > [dog age])
{
return NSOrderedDescending;
}
else if(_age == [dog age])
{
return NSOrderedSame;
}
else
{
return NSOrderedAscending;
}
}
-(BOOL)sortByAgeWithOtherDog:(Dog *)dog
{
return _age < [dog age];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"dog age: %5ld weight: %5ld",_age,_weight];
}
@end