Multi-Dimensonal Arrays - Objective C
Arrays are important to Objective C and need lots of more details. There are following few important concept related to array which should be clear to a objective C programmer:
Objective C support multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array.
Objective C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration:
For example, the following declaration creates a three dimensional 5, 10 ,4 integer array:
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-demensional integer array of size x.y. You would write something as follows:
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 and y number of columns. A 2-dimensional array a, which contains three rows and four columns.
Thus, every elements in array a is identified by an element name of the form a[i][j], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Initializing Two Dimensional Arrays
Multidimensional array may be initialized by specifying bracketed values for each now. Following is an array with 3 rows and each rows has 4 columns
The nested braces, which indicates the intended rows are optional. The following initialization is equivalent to previous example.
Accessing Two Dimensional Array Element
An element in 2 dimensional array is accessed by using the subscripts, i.e row index and column index of the array. For example:
Objective C support multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array.
Objective C programming language allows multidimensional arrays. Here is the 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-demensional 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 and y number of columns. A 2-dimensional array a, which contains three rows and four columns.
Thus, every elements in array a is identified by an element name of the form a[i][j], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Initializing Two Dimensional Arrays
Multidimensional array may be initialized by specifying bracketed values for each now. Following is an array with 3 rows and each rows has 4 columns
int a [3][4] = [
{0, 1, 2, 3} , //initializer for rows indexed by 0
{4, 5, 6, 7} , //initializer for rows indexed by1
{8, 9, 10, 11} //initializer for rows indexed by2
};
{0, 1, 2, 3} , //initializer for rows indexed by 0
{4, 5, 6, 7} , //initializer for rows indexed by1
{8, 9, 10, 11} //initializer for rows indexed by2
};
The nested braces, which indicates the intended rows 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 Element
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.
No comments: