I am new to Objective-C and can not understand the difference in using curly brackets and square brackets for arrays? For example below code
float trainingData[3][3] = { { 84, 191, 19 }, { 24, 186, 17}, { 22, 157, 21} };
you have a multi dimensional array or jaggad array,which means an array of arrays...
this means you have....................................with values
array1: x1 x2 x3 84,191,19
array2: x4 x5 x6 24,186,17
array3: x7 x8 x9 22,157,21
now the curly brackets represents which area you are adding
so if you add row 1(or array 1) ... trainingData[0]
It requires an array of values(a row has multiple values)
So the curly brackets mean you are assigning an object in its index
for example if you want an array with values 1,2,3 you would do something like
int[] values = {1,2,3}
now in your case you have an array of array's so the first(or outside) curly brackets are for defining each row and the curly brackets within them is to define each row's values inside them
example
float trainingData[3][2] = { { 84, 191}, { 24, 186}, { 22, 157} };
note now the each row only has 2 values, because the array length has changed so you have 3 groups of 2
array1: 84,191
array2: 24,186
array3: 22,157
In C:
Square Bracket: It represent that it is an Array.
In your given code trainingData[3][3] represent the a 2D Array with 3 row and 3 column.
Curly Bracket: Multidimensional arrays may be initialized by specifying bracketed values for each row. In your Given code:
float trainingData[3][3] = {
{ 84, 191, 19 }, /* initializers for row indexed by 0 */
{ 24, 186, 17}, /* initializers for row indexed by 1 */
{ 22, 157, 21} /* initializers for row indexed by 2 */
};
In Objective C:
Square Bracket:
the square bracket syntax use for all of method calls. you use the dot syntax when you're using getters and setters.
Curly Bracket: It Represent the dictionary or mutable dictionary.
NSDictionary *Dic= @{@"Name":@"abc",@"ID":@"123"};
You can says that declaration or definition take places in square brackets and values assigns in curly brackets.
for example,
double balance[5];
This is the array of size 5
, so that 5
is defined in square brackets
.
Now,
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
then value is define in curly bracket.
balance[4] = 50.0;
setting 4th element, so index is define in square bracket. Here there is a single value so curly bracket is not require. So, basically use of curly bracket is to declare or assign set. As you have post array in your question is also value set of array.
so, you can differentiate it like i have mentioned.
Actually these are the protocols or syntax you said to use array, so finding difference between use of braces is meaningless actually.
float trainingData[3][3] is a multi dimentional array you are having and you are saying trainingData multi dimentional array is of size 3 : 3 within the square brackets
within the curly braces { { 84, 191, 19 }, { 24, 186, 17}, { 22, 157, 21} };
you are assigning values for the array
[ 3 : 3 ]
trainingData{0} => { 84, 191, 19 }
trainingData{1} => { 24, 186, 17}
trainingData{3} => { 22, 157, 21}
Usually you define the size with square brackets and assing them with curly braces
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.