14.5.10.2 Array creation expressions

  An array-creation-expression is used to create a new instance of an 
  Array-type. 
  Array-creation-expression: 
  New non-array-type [expression-list] rank-specifiersopt 
  Array-initializeropt 
  New array-type array-initializer 
  An array creation expression of the first form allocates an array instance 
  Of the type that results from 
  Deleting each of the individual expressions from the expression list. 
  [Example: For example, the array 
  Creation expression new int [10, 20] produces an array instance of type 
  Int [], and the array creation 
  Expression new int [10] [,] produces an array of type int [][,]. end example] 
  Each expression in the 
  Expression list must be of type int, uint, long, or ulong, or of a type 
  That can be implicitly converted to 
  One or more of these types. The value of each expression determines the 
  Length of the corresponding 
  Dimension in the newly allocated array instance. Since the length of an 
  Array dimension must be 
  C # LANGUAGE SPECIFICATION 
148
  Nonnegative, it is a compile-time error to have a constant expression with 
  A negative value, in the expression 
  List. 
  Except in an unsafe context (? 5.1), the layout of arrays is unspecified. 
  If an array creation expression of the first form includes an array 
  Initializer, each expression in the 
  Expression list must be a constant and the rank and dimension lengths 
  Specified by the expression list must 
  Match those of the array initializer. 
  In an array creation expression of the second form, the rank of the 
  Specified array type must match that of 
  The array initializer. The individual dimension lengths are inferred from 
  The number of elements in each of 
  The corresponding nesting levels of the array initializer. Thus, the 
  Expression 
  New int [] ((0, 1), (2, 3), (4, 5)) 
  Exactly corresponds to 
  New int [3, 2] ((0, 1), (2, 3), (4, 5)) 
  Array initializers are described further in? 9.6. 
  The result of evaluating an array creation expression is classified as a 
  Value, namely a reference to the newly 
  Allocated array instance. The run-time processing of an array creation 
  Expression consists of the following 
  Steps: 
  ? The dimension length expressions of the expression-list are evaluated in 
  Order, from left to right. 
  Following evaluation of each expression, an implicit conversion (? 3.1) to 
  One of the following types is 
  Performed: int, uint, long, ulong. The first type in this list for which an 
  Implicit conversion exists is 
  Chosen. If evaluation of an expression or the subsequent implicit 
  Conversion causes an exception, then 
  No further expressions are evaluated and no further steps are executed. 
  ? The computed values for the dimension lengths are validated, as follows: 
  If one or more of the values 
  Are less than zero, a System.OverflowException is thrown and no further 
  Steps are executed. 
  ? An array instance with the given dimension lengths is allocated. If there 
  Is not enough memory available 
  To allocate the new instance, a System.OutOfMemoryException is thrown and 
  No further steps are 
  Executed. 
  ? All elements of the new array instance are initialized to their default 
  Values (? 2.2). 
  ? If the array creation expression contains an array initializer, then each 
  Expression in the array initializer 
  Is evaluated and assigned to its corresponding array element. The 
  Evaluations and assignments are 
  Performed in the order the expressions are written in the array 
  Initializer? In other words, the elements are 
  Initialized in increasing index order, with the rightmost dimension 
  Increasing first. If evaluation of a 
  Given expression or the subsequent assignment to the corresponding array 
  Element causes an exception, 
  Then no further elements are initialized (and the remaining elements will 
  Thus have their default values). 
  An array creation expression permits instantiation of an array with 
  Elements of an array type, but the 
  Elements of such an array must be manually initialized. [Example: For 
  Example, the statement 
  Int [] [] a = new int [] [100]; 
  Creates a single-dimensional array with 100 elements of type int []. The 
  Initial value of each element is 
  Null. End example] It is not possible for the same array creation 
  Expression to also instantiate the subarrays, 
  And the statement 
  Int [] [] a = new int [100] [5]; / / Error 
  Results in a compile-time error. Instantiation of the sub-arrays must 
  Instead be performed manually, as in 
  Int [] [] a = new int [] [100]; 
  For (int i = 0; i <100; i + +) a [i] = new int [5]; 
  When an array of arrays has a? Rectangular? Shape, and that is when the 
  Sub-arrays are all of the same length, it is 
  More efficient to use a multi-dimensional array. In the example above, 
  Instantiation of the array of arrays 
  Creates 101 objects? One outer array and 100 sub-arrays. In contrast, 
  Chapter 14 Expressions 
149
  Int [] = new int [100, 5]; 
  Creates only a single object, a two-dimensional array, and accomplishes the 
  Allocation in a single statement. 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply