9_Objective-C Arrays

What is an Array

Collection of variables of the same type.

Type of arrays

  • Single-dimensional array.
  • Multidimensional array

single-dimensional array.

Declaring Arrays

type arrayName [ arraySize ];
  • arraySize must be an integer constant greater than zero and
  • type can be any valid Objective-C data type.
double balance[10];

Now, balance is a variable array, which is sufficient to hold up to 10 double numbers.

Initializing Arrays

Array name[ index_number ] = value;


type arrayname[array_length] = {value_1,value_2,etc....};

The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the initialization is created.

Therefore, if you write:

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example.

balance[4] = 50.0;

The above statement assigns element number 5th in the array a value of 50.0.

Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index.

Accessing Array Elements

An element is accessed by indexing the array name.

This is done by placing the index of the element within square brackets after the name of the array.

For example:

double salary = balance[9];

The above statement will take 10th element from the array and assign the value to salary variable.

Example single-dimensional array.

Multi-dimensional arrays in Objective-C

general form of a multidimensional array declaration:

type name[size1][size2]...[sizeN];

For example, the following declaration creates a three-dimensional 5, 10, 4 integer array:

int threedim[5][10][4];

Two-Dimensional Arrays:

The simplest form of the multidimensional array is the two-dimensional array.

A two-dimensional array is, in essence, a list of one-dimensional arrays.

To declare a two-dimensional integer array of size x,y, you would write something as follows:

type arrayName [ x ][ y ];

Where type can be any valid Objective-C data type and arrayName will be a valid Objective-C identifier.
A two-dimensional array can be think as a table, which will
  • have x number of rows
  • y number of columns.

Initializing Two-Dimensional Arrays:

Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns.

int a[3][4] = {  
 {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
 {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
 {8, 9, 10, 11}   /*  initializers for row indexed by 2 */

The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example:

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements:

An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example:

int val = a[2][3];

The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram. Let us check below program where we have used nested loop to handle a two-dimensional array:

Example of 2-dimensional array

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s