【Foundation Frame】NSArray

NSArray
代表元素有序、可重复的一个集合,集合中每个元素都有对应的顺序索引。

创建NSArray的常见方法
1、array:创建一个不包含任何元素的空NSARRAY
2、arrayWithObject/initWithObject:创建只包含指定元素的NSArray
3、arrayWithObjects/initWithObjects:创建只包含一个或多个元素的NSArray
4、arrayWithContentsOfFile:读取文件内容来创建NSArray

(以下程序列举了常用的NSArray方法)
//
//  main.m
//  Array
//
//  Created by mac on 14-12-3.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import <Foundation/Foundation.h>
#import
"People.h"

//静态创建数组的方法
void staticCreatArray()
{
   
NSArray * array1 = [NSArray arrayWithObject:@"NSArray"];
   
NSLog(@"Way 1: %@",array1);
   
   
NSArray * array2 = [NSArray arrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3", nil];
   
NSLog(@"Way 2: %@",array2);
   
   
NSArray * array3 = [NSArray arrayWithArray:array2];
   
NSLog(@"Way 3: %@",array3);
  
    [array1
release];
    [array2
release];
    [array3
release];

}

//动态创建数组的方法
void dynamicCreatArray()
{
   
NSArray * array1 = [[NSArray alloc]initWithObjects:@"1,2,3,4,5",@"HELLO", nil];
   
NSLog(@"Way 1: %@",array1);
   
   
NSArray * array2 = [[NSArray alloc]initWithArray:array1];
   
NSLog(@"Way 2: %@",array2);
   
}

//查询集合元素在NSArray中的序列
void searchArray()
{
   
NSArray * array1 = [NSArray arrayWithObject:@"NSArray"];
   
NSArray * array2 = [NSArray arrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3", nil];
   
NSArray * array3 = [[NSArray alloc]initWithObjects:@"1,2,3,4,5",@"HELLO",@"HELLO", nil];
   
   
NSLog(@"%lu",[array1 count]);
   
NSLog(@"%hhd",[array3 containsObject:@"HELLO"]);
   
NSLog(@"%@",[array2 lastObject]);
   
NSLog(@"%@",[array2 objectAtIndex:1]);
   
NSLog(@"%lu",[array2 indexOfObject:@"NSArray1"]);
   
NSRange range = NSMakeRange(0, 2);
   
NSLog(@"%lu",[array2 indexOfObject:@"NSArray2" inRange:range]);
   
    [array3
release];
   
}

//对数组进行比较
void compareArray()
{
   
NSArray * array1 = [NSArray arrayWithObject:@"NSArray"];
   
NSArray * array11 = [array1 copy];
   
NSArray * array2 = [NSArray arrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3", nil];
   
NSArray * array3 = [[NSArray alloc]initWithObjects:@"1,2,3,4,5",@"HELLO",@"HELLO",@"NSArray1", nil];
   
   
NSLog(@"%hhd",[array1 isEqualToArray:array11]);
   
NSLog(@"%@",[array2 firstObjectCommonWithArray:array3]);
    [array3
release];
}

//遍历数组
void ergodicArray()
{
  
NSArray * array2 = [NSArray arrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3", nil];
   
//C语言方式
   
for (int i = 0; i<[array2 count]; i++)
    {
       
NSLog(@"%@",[array2 objectAtIndex:i]);
    }
   
//快速遍历
   
for (id obj in array2)
    {
       
NSLog(@"    %@",obj);
       
    }
   
//Block方法遍历
    [array2
enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop)
    {
       
if (idx<1)
        {
            * stop =
YES ;
        }
       
       
NSLog(@"%lu,%@",idx,obj);
    }];
   
   
//迭代器方法遍历
   
NSEnumerator * enumerator =[array2 objectEnumerator];
   
id obj ;
   
while (obj= [enumerator nextObject]) {
       
NSLog(@"%@",obj);
    }
}

//其他方法
void otherArray()
{
   
NSArray * array1 = [NSArray arrayWithObject:@"NSArray"];
   
NSArray * array2 = [NSArray arrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3", nil];
   
   
//快速遍历array
   
for (id obj in array2)
    {
       
NSLog(@"%@",obj);
       
    }
   
    array2 = [array2
arrayByAddingObject:@"NSArray4"];
   
for (id obj in array2)
    {
       
NSLog(@"  %@",obj);
       
    }
   
    array2 = [array2
arrayByAddingObjectsFromArray:array1];
   
for (id obj in array2)
    {
       
NSLog(@"    %@",obj);
       
    }
   
   
NSArray *pathArray = [NSArray arrayWithObjects:@"here", @"be", @"dragons", nil];
   
NSLog(@"%@",pathArray);
   
NSLog(@"%@",[pathArray componentsJoinedByString:@"->"]);
   
   
NSRange range = NSMakeRange(0, 3);
   
NSLog(@"%@",[array2 subarrayWithRange:range]);
   
}

//排序
void sortArray()
{
   
NSArray * array2 = [NSArray arrayWithObjects:@"NSArray2",@"NSArray1",@"NSArray3", nil];
   
NSLog(@"Before Sort:%@",[array2 componentsJoinedByString:@" "]);
   
//选择排序
   
NSArray * selectSortArray = [array2 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
   
NSLog(@"After Sort :%@",[selectSortArray componentsJoinedByString:@" "]);
   
   
NSArray * array3 = [NSArray arrayWithObjects:@"NSArray2",@"NSArray1",@"NSArray3",@"NSArray5",@"NSArray4", nil];
   
NSLog(@"Before Sort:%@",[array3 componentsJoinedByString:@" "]);
   
   
}

int main(int argc, const char * argv[]) {
   
@autoreleasepool {
      
// insert code here...
      
// NSLog(@"Hello, World!");
      
// staticCreatArray();
      
//  dynamicCreatArray();
      
// searchArray();
      
//  compareArray();
      
// ergodicArray();
       //  otherArray();
       // sortArray();
       
    }
   
return 0;
}



posted @ 2014-12-03 18:04  shujucn  阅读(158)  评论(0编辑  收藏  举报