Regards to the following code:
int[] to = new int[] { text };
I understand it tries to define an array of integer, but What does the curly braces do in array definition?
The elements in cell arrays are cells. These cells can contain different types of values. With cell arrays, you can refer to the cells, or to the contents of the cells. Using curly braces for the subscripts will reference the contents of a cell; this is called content indexing.
We can construct a cell array using curly brackets: '{' and '}'. Recall that we normally use the square brackets '[' and ']' to construct an ordinary array (e.g. see Example 5.7). Here, we initialize a cell array to contain three strings with different lengths.
If the number of statements following the for/if is single you don't have to use curly braces. But if the number of statements is more than one, then you need to use curly braces.
The square brackets are used for declaring an array, the type ( int in our case) tells us what type of values the array will hold. An array is an object and therefore it is created with the new keyword. a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5; We initialize the array with some data.
This is just a shortcut code to create an array with initial elements, the followings (which are equal):
int[] to = new int[] { text }; int[] to = { text };
can be substituted with
int[] to = new int[1]; to[0] = text;
Hope this helps.
The curly braces contain values to populate the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With