Knowledge about arrays

Source: Internet
Author: User
Tags array sort

[OBJC] View plaincopy

  1. To create an empty array

  2. Nsarray *array = [Nsarray array];

  3. Create an array with 1 elements

  4. Array = [Nsarray arraywithobject:@ "123"];

  5. To create an array with multiple elements

  6. Array = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", nil Nil];

  7. Nsarray *array3 = [Array Arraybyaddingobjectsfromarray:[nsarray arraywithobjects:@ "4", @ "5", nil Nil]];

  8. Nsarray *array4 = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil Nil];

  9. Nsrange range = Nsmakerange (1, 2);

  10. Nsarray *array5 = [Array4 subarraywithrange:range];

2. Some basic methods of arrays

[OBJC] View plaincopy

  1. int count = [array count];//number

  2. Determine if an element is included

  3. if ([Array containsobject:@ "a"]) {

  4. NSLog (@ "contains string a");

  5. }

  6. NSString *last = [array lastobject]; last element

  7. NSString *str = [array objectatindex:1]; Gets the elements in the array according to the index

  8. int index = [array indexofobject:@ "C"]; Gets the index of the specified element

  9. Let all objects inside the array call the test method, 123 is the parameter

  10. Nsarray *array = [Nsarray arraywithobjects:stu1, STU2, STU3, nil Nil];

  11. [Array makeobjectsperformselector: @selector (test2:) withobject:@ "123"];

  12. Nsarray *array = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil Nil];

  13. 1-2-3-4

  14. Use separators-to stitch all array elements

  15. NSString *str = [Array componentsjoinedbystring:@ "-"];

  16. Writes an array to a file (an XML file is generated)

  17. NSString *path = @ "/users/apple/desktop/array.xml";

  18. [Array Writetofile:path atomically:yes];

  19. Path = @ "/users/apple/desktop/array.txt";

  20. Reading array contents from a file (file has strict formatting requirements)

  21. Nsarray *array2 = [Nsarray Arraywithcontentsoffile:path];

3. Iterating through an array

[OBJC] View plaincopy

  1. #pragma mark iterates through the array 1

  2. void ArrayFor1 () {

  3. Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];

  4. int count = Array.count;

  5. for (int i = 0; i<count; i++) {

  6. ID obj = [array objectatindex:i];

  7. }

  8. }

  9. #pragma mark iterates through the array 2 fast traversal

  10. void ArrayFor2 () {

  11. Student *STU1 = [Student Student];

  12. Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];

  13. int i = 0;

  14. for (ID obj in array) {

  15. NSLog (@ "%i-%@", I, obj);

  16. i++;

  17. }

  18. }

  19. #pragma mark iterates through the array 3

  20. void ArrayFor3 () {

  21. Student *STU1 = [Student Student];

  22. Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];

  23. [Array Enumerateobjectsusingblock:

  24. ^ (ID obj, Nsuinteger idx, Boolbool *stop) {

  25. NSLog (@ "%i-%@", idx, obj);

  26. If the index is 1, stop traversing

  27. if (idx = = 1) {

  28. Using pointers to modify the value of a bool variable outside

  29. *stop = YES;

  30. }

  31. }];

  32. }

  33. #pragma mark iterates through the array 4

  34. void ArrayFor4 () {

  35. Student *STU1 = [Student Student];

  36. Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];

  37. Get an iterator to an array

  38. Nsenumerator *enumerator = [array objectenumerator];

  39. Reverse-order iterator (traversing elements from the tail)

  40. Nsenumerator *enumerator = [array reverseobjectenumerator];

  41. AllObjects is to remove objects that have not been traversed

  42. Nsarray *array2 = [enumerator allobjects];

  43. NSLog (@ "array2:%@", array2);

  44. Gets the next element to traverse

  45. ID obj = nil;

  46. while (obj = [Enumerator nextobject]) {

  47. NSLog (@ "obj=%@", obj);

  48. }

  49. }



4. Array sorting

[OBJC] View plaincopy

    1. #pragma mark Array Sort 1

    2. void ArraySort1 () {

    3. Nsarray *array = [Nsarray arraywithobjects:@ "2", @ "3", @ "1", @ "4", nil Nil];

    4. Returns an ordered array that does not change the order of the elements in the original array

    5. The comparison method for the specified element: compare:

    6. Nsarray *array2 = [Array sortedarrayusingselector: @selector (compare:)];

    7. NSLog (@ "array2:%@", array2);

    8. }

    9. #pragma mark Array Sort 2

    10. void ArraySort2 () {

    11. Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"];

    12. Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"];

    13. Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"];

    14. Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"];

    15. Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];

    16. Specify a comparison method for sorting

    17. Nsarray *array2 = [Array sortedarrayusingselector: @selector (comparestudent:)];

    18. NSLog (@ "array2:%@", array2);

    19. }

    20. -(Nscomparisonresult) Comparestudent: (Student *) Stu {

    21. Sort by last name first

    22. Nscomparisonresult result = [Self.lastname compare:stu.lastname];

    23. If you have the same last name, compare the names.

    24. if (result = = Nsorderedsame) {

    25. result = [Self.firstname compare:stu.firstname];

    26. }

    27. return result;

    28. }

    29. #pragma mark Array Sort 3

    30. void ArraySort3 () {

    31. Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"];

    32. Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"];

    33. Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"];

    34. Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"];

    35. Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];

    36. Sort by block

    37. Nsarray *array2 = [Array sortedarrayusingcomparator:

    38. ^nscomparisonresult (Student *obj1, Student *obj2) {

    39. Sort by last name first

    40. Nscomparisonresult result = [Obj1.lastname compare:obj2.lastname];

    41. If you have the same last name, compare the names.

    42. if (result = = Nsorderedsame) {

    43. result = [Obj1.firstname compare:obj2.firstname];

    44. }

    45. return result;

    46. }];

    47. NSLog (@ "array2:%@", array2);

    48. }

    49. #pragma mark Array Sort 4-advanced sort

    50. void ArraySort4 () {

    51. Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li" bookname:@ "Book1"];

    52. Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang" bookname:@ "Book2"];

    53. Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li" bookname:@ "Book2"];

    54. Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao" bookname:@ "Book1"];

    55. Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];

    56. 1. Sort by Title first

    57. The key here is the name of @property.

    58. Nssortdescriptor *booknamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "Book.name" Ascending:YES];

    59. 2. Sort by last Name

    60. Nssortdescriptor *lastnamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "LastName" Ascending:yes];

    61. 3. Sort by name again

    62. Nssortdescriptor *firstnamedesc = [nssortdescriptor sortdescriptorwithkey:@ "FirstName" Ascending:yes];

    63. Add a sort descriptor sequentially

    64. Nsarray *descs = [Nsarray arraywithobjects:booknamedesc, Lastnamedesc, Firstnamedesc, nil Nil];

    65. Nsarray *array2 = [array Sortedarrayusingdescriptors:descs];

    66. NSLog (@ "array2:%@", array2);

    67. }

Knowledge about arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.