Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curly braces when define array

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?

like image 580
Leem.fin Avatar asked Feb 02 '12 14:02

Leem.fin


People also ask

How are curly braces used for an array?

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.

Which brackets used to define array?

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.

Which loops require curly braces?

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.

What type of brackets are used in Java to initialize array?

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.


2 Answers

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.

like image 67
Egor Avatar answered Sep 22 '22 14:09

Egor


The curly braces contain values to populate the array.

like image 26
SLaks Avatar answered Sep 24 '22 14:09

SLaks