Arrays in Objective-C
An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming language.
Declaration of an Array
To declare an array we must have to specify the type of the array along with the name and size of the array as shown in the below syntax:
Syntax:
type arr[size-of-array];
Here, the above syntax is of a one-dimensional array, where type can be any valid data type and size-of-array defines the size of the array in memory.
For example:
int arr[5];
Here, the above array is of integer type with size 5 which means it holds up to 5 integer elements.
Example:
ObjectiveC
// Objective-C program of an array #import <Foundation/Foundation.h> int main ( int argc, const char * argv[]) { NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init]; // Declaring the array of int type along // with the size of the array that is 5 int arr[5] = {10, 20, 30, 40, 50}; int i; // Accessing the array using loop for (i = 0; i < 5; i++) { // Printing the Array printf( "%d " , arr[i]); } [pool drain]; return 0; } |
Output:
10 20 30 40 50
Initializing an Arrays
In objective C, you can initialize the array either one by one or complete the array together(Or we can say multiple values together). To initialize an array one by one we have to assign the value at the specified index. For example:
int ArrayName[3] = 45
Here, the value 45 is initialized at index 3 in the array.
To initialize the complete array together we have to assign all the values using curly braces({}). The length of the curly braces is the same as the size of the array means if the size of the array is 10 then the curly braces stores only 10 elements. For example:
int ArrayName[5] = {2, 5, 7, 3, 2}
Here the size of the array is 5 so we store five integer-type elements in it.
If we omit the size of the array, then the compiler can create an array just big enough to hold the initialization.
int arr[] = {55, 78, 89, 99, 250} ;
Example 1:
ObjectiveC
// Objective-C program of an array initialization #import <Foundation/Foundation.h> int main ( int argc, const char * argv[]) { NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init]; // Here we are initialize the array using curly braces int arr[5] = {33, 25, 45, 78, 88}; // Here we are printing the array printf( "%d " , arr[0]); printf( "%d " , arr[1]); printf( "%d " , arr[2]); printf( "%d " , arr[3]); printf( "%d " , arr[4]); [pool drain]; return 0; } |
Output:
33 25 45 78 88
Example 2:
ObjectiveC
// Objective-C program of an array initialization #import <Foundation/Foundation.h> int main ( int argc, const char * argv[]) { NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init]; // Here, we are omit the size of array // then we are initializing the array int arr[] = {55, 78, 89, 99, 250}; // Here we are printing the array printf( "%d " , arr[0]); printf( "%d " , arr[1]); printf( "%d " , arr[2]); printf( "%d " , arr[3]); printf( "%d " , arr[4]); [pool drain]; return 0; } |
Output:
55 78 89 99 250
Accessing Array Elements
We can access the array elements using indexes. So write the index number between square brackets after the array’s name. As shown in the below example:
int rollno = roll_no_array[2];
Here, we are accessing the “roll no” from the roll no array using the index. As we know that an array always starts from zero. So we are here accessing the third element of the array.
Example:
ObjectiveC
// Objective-C program to access array using index #import <Foundation/Foundation.h> int main ( int argc, const char * argv[]) { NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init]; // Roll no array declaration int roll_no_array[] = {55, 78, 89, 99, 250}; // Accessing the array element from the roll // no array elements is 89 int rollno = roll_no_array[2]; // Print the element printf( "Roll No is = %d " , rollno); [pool drain]; return 0; } |
Output:
Roll No is = 89
Access an array of elements via Loop
We can also access the array using loops. Loops are a very efficient way to access the array because if we have 100 elements in the array, then it’s very difficult to access all elements via indexes. So using for loop we can iterate through each element of the array. We can also reverse the arrays using loops.
Example:
ObjectiveC
// Objective-C program to access array using loop #import <Foundation/Foundation.h> int main ( int argc, const char * argv[]) { NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init]; // Roll no array declaration int roll_no_array[5] = {55, 78, 89, 99, 250}, i; // Here we are accessing the roll no using loop printf( "Roll No is = " ); for (i = 0; i < 5; i++) printf( "%d " , roll_no_array[i]); [pool drain]; return 0; } |
Output:
Roll No is = 55 78 89 99 250
Contact Us