IOS array Traversal

Source: Internet
Author: User
Http://www.cnblogs.com/love-lie/archive/2012/07/11/2585804.html
The array created using an array object is very powerful. The array defined in Java or C must be of the same type as each element in the array. The objective-C language can put any type of data in the array. It is worth noting that only the pointer pointing to this object can be put. If int, Char, double, etc. are put directly, it cannot be done.

  1. Immutable array object  The nsarray keyword is used to create an unchangeable array. After initialization, the elements of this array cannot be dynamically added or deleted. Create a myclass object and store the pointer to this object in the nsarray array. It can be seen that the nsarray object array can store data of any object.   [Arraycount]: Get the length of the array of this object. [Arrayobjectatindex 0]: input an array script ID to obtain the data object. [Arraywithobjects;...]: assigns a value to the array object initialization. Here, we can write a pointer to any object, and nil must be used at the end. Mark end ~
  Use the for loop to traverse this array object.
  1. # Import <uikit/uikit. h>
  2. # Import "myclass. H"
  3. Intmain (intargc,
    Char * argv [])
  4. {
  5. NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc] init];
  6. // Add our test code
  7. // Create a custom class
  8. Myclass * myclass = [[myclassalloc] init];
  9. // Create an unchangeable Array
  10. Nsarray * array = [nsarray arraywithobjects: @ "Yu Song", @ "momo", @ "", myclass, nil];
  11. // Obtain the length of the array
  12. Int COUNT = [arraycount];
  13. // Traverse this array
  14. For (INT I = 0; I <count; I ++ ){
  15. Nslog (@ "normal traversal: When I = % d, the array object is: % @", I, [array objectatindex: I]);
  16. }
  17. Intretval = uiapplicationmain (argc, argv, nil, nil );
  18. [Pool release];
  19. Returnretval;
  20. }
 

The above for loop Traversal method is common, but the efficiency is not good, and the security is not high. Objective-C provides a fast enumeration Traversal method, which is recommended for traversal. For (nsobject * object in array): traverses the array. Each loop assigns the elements in the array to * object for loop traversal.# Import <uikit/uikit. h>

  1. # Import "myclass. H"
  2. Intmain (intargc,
    Char * argv [])
  3. {
  4. NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc] init];
  5. // Add our test code
  6. // Create a custom class
  7. Myclass * myclass = [[myclassalloc] init];
  8. // Create an unchangeable Array
  9. Nsarray * array = [nsarray arraywithobjects: @ "Yu Song", @ "momo", @ "", myclass, nil];
  10. // Traverse this array
  11. For (nsobject * object in array ){
  12. Nslog (@ "Fast traversal array object: % @", object );
  13. }
  14. Int retval = uiapplicationmain (argc, argv, nil, nil );
  15. [Pool release];
  16. Returnretval;
  17. }

2. variable array objects  [Nsmutablearray arraywithcapacity: 10]: initializes the length of a variable array object. If the subsequent Code adds an array that exceeds the length of 10 The length of nsmutablearray is automatically expanded, so you don't have to worry about array out-of-bounds. [Array addobject:...]: Add a data object to the end of the variable array. [Array addobjectsfromarray: ..]: adds an array object to the end of a variable array.*) #>];
  1. # Import <uikit/uikit. h>
  2. # Import "myclass. H"
  3. Intmain (intargc,
    Char * argv [])
  4. {
  5. NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc] init];
  6. // Add our test code
  7. // Create a custom class
  8. Myclass * myclass = [[myclassalloc] init];
  9. // Create a variable array with a length of 10
  10. Nsmutablearray * array = [nsmutablearrayarraywithcapacity: 10];
  11. // Add objects to the array dynamically
  12. [Array addobject: @ "Yu Song"];
  13. [Array addobject: @ "momo"];
  14. [Array addobject: @ "cute"];
  15. [Array addobject: @ ""];
  16. [Array addobject: myclass];
  17. For (nsobject * object in array)
  18. {
  19. Nslog (@ "output object array: % @", object );
  20. }
  21. Intretval = uiapplicationmain (argc, argv, nil, nil );
  22. [Pool release];
  23. Returnretval;
  24. }
 
  [Array insertobject: @ "insert a tiger" atindex: 1]: Insert a String object into the variable array and set its position index to 1.

[Array insertobject :( nsarray *) atindex :( nsindexset *)]:Insert an array object into a variable array. You can set its position index.

  1. # Import <uikit/uikit. h>
  2. # Import "myclass. H"
  3. Intmain (intargc,
    Char * argv [])
  4. {
  5. NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc] init];
  6. // Add our test code
  7. // Create a custom class
  8. Myclass * myclass = [[myclassalloc] init];
  9. // Create a variable array with a length of 10
  10. Nsmutablearray * array = [nsmutablearrayarraywithcapacity: 10];
  11. // Add objects to the array dynamically
  12. [Array addobject: @ "Yu Song"];
  13. [Array addobject: @ "momo"];
  14. [Array addobject: @ "cute"];
  15. [Array addobject: @ ""];
  16. [Array addobject: myclass];
  17. // Insert to the array
  18. [Array insertobject: @ "insert a tiger" atindex: 1];
  19. For (nsobject * objectin array)
  20. {
  21. Nslog (@ "output object array: % @", object );
  22. }
  23. Int retval = uiapplicationmain (argc, argv, nil, nil );
  24. [Pool release];
  25. Returnretval;
  26. }
 
  Nsange range = nsmakerange (); set a range between 0 and 5. [Array removeobject: myclass inrange: range]: sets to delete data within a range. If this range does not delete this object, nothing will be deleted. In this example, because the myclass object is in the range of 0 to 5 of the array, myclass is deleted.   [Array removeobject :( ID)]: You do not need to consider the range. This object exists in the main array and will be deleted directly. [Arrayremoveobjectatindex :( nsuinteger)]: deletes the data of the specified index.   [Array removeobjectidenticalto :( ID)]: deletes the specified Element in the array. [Array removeobjectidenticalto :( ID) inrange :( nsange)]: deletes the specified element within the specified range.   [Array removeobjectsinarray :( nsarray *)]: deletes an array element.
  1. # Import <uikit/uikit. h>
  2. # Import "myclass. H"
  3. Intmain (intargc,
    Char * argv [])
  4. {
  5. NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc] init];
  6. // Add our test code
  7. // Create a custom class
  8. Myclass * myclass = [[myclassalloc] init];
  9. // Create a variable array with a length of 10
  10. Nsmutablearray * array = [nsmutablearrayarraywithcapacity: 10];
  11. // Add objects to the array dynamically
  12. [Array addobject: @ "Yu Song"];
  13. [Array addobject: @ "momo"];
  14. [Array addobject: @ "cute"];
  15. [Array addobject: @ ""];
  16. [Array addobject: myclass];
  17. // Set a deletion range
  18. Nsange range = nsmakerange (0, 5 );
  19. // Delete an element
  20. [Array removeobject: myclassinrange: range];
  21. For (nsobject * object in array)
  22. {
  23. Nslog (@ "output object array: % @", object );
  24. }
  25. Intretval = uiapplicationmain (argc, argv, nil, nil );
  26. [Pool release];
  27. Returnretval;
  28. }
 
  [Array replaceobjectatindex 2 withobject: @ "modified"]: Replace the data marked as 2 in the array.
  1. # Import <uikit/uikit. h>
  2. # Import "myclass. H"
  3. Intmain (intargc,
    Char * argv [])
  4. {
  5. NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc] init];
  6. // Add our test code
  7. // Create a custom class
  8. Myclass * myclass = [[myclassalloc] init];
  9. // Create a variable array with a length of 10
  10. Nsmutablearray * array = [nsmutablearrayarraywithcapacity: 10];
  11. // Add objects to the array dynamically
  12. [Array addobject: @ "Yu Song"];
  13. [Array addobject: @ "momo"];
  14. [Array addobject: @ "cute"];
  15. [Array addobject: @ ""];
  16. [Array addobject: myclass];
  17. // Modify the element whose index is 2
  18. [Array replaceobjectatindex: 2 withobject: @ "modified"];
  19. For (nsobject * object in array)
  20. {
  21. Nslog (@ "output object array: % @", object );
  22. }
  23. Intretval = uiapplicationmain (argc, argv, nil, nil );
  24. [Pool release];
  25. Returnretval;
  26. }

Related Article

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.