19.6 Array initializers

原创 2006年05月25日 05:47:00
Array initializers may be specified in field declarations (§17.4), local
variable declarations (§15.5.1), and
array creation expressions (§14.5.10.2):
array-initializer:
{ variable-initializer-listopt }
{ variable-initializer-list , }
variable-initializer-list:
variable-initializer
variable-initializer-list , variable-initializer
variable-initializer:
expression
array-initializer
An array initializer consists of a sequence of variable initializers,
enclosed by ?{?and ?}? tokens and
separated by ?,? tokens. Each variable initializer is an expression or, in
the case of a multi-dimensional
array, a nested array initializer.
The context in which an array initializer is used determines the type of
the array being initialized. In an array
creation expression, the array type immediately precedes the initializer.
In a field or variable declaration, the
array type is the type of the field or variable being declared. When an
array initializer is used in a field or
variable declaration, [Example: such as:
int[] a = {0, 2, 4, 6, 8};
end example] it is simply shorthand for an equivalent array creation
expression: [Example:
int[] a = new int[] {0, 2, 4, 6, 8};
end example]
For a single-dimensional array, the array initializer must consist of a
sequence of expressions that are
assignment compatible with the element type of the array. The expressions
initialize array elements in
increasing order, starting with the element at index zero. The number of
expressions in the array initializer
determines the length of the array instance being created. [Example: For
example, the array initializer above
creates an int[] instance of length 5 and then initializes the instance
with the following values:
a[0] = 0; a[1] = 2; a[2] = 4; a[3] = 6; a[4] = 8;
end example]
For a multi-dimensional array, the array initializer must have as many
levels of nesting as there are
dimensions in the array. The outermost nesting level corresponds to the
leftmost dimension and the
innermost nesting level corresponds to the rightmost dimension. The length
of each dimension of the array is
determined by the number of elements at the corresponding nesting level in
the array initializer. For each
nested array initializer, the number of elements must be the same as the
other array initializers at the same
level. [Example: The example:
int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
creates a two-dimensional array with a length of five for the leftmost
dimension and a length of two for the
rightmost dimension:
C# LANGUAGE SPECIFICATION
278
int[,] b = new int[5, 2];
and then initializes the array instance with the following values:
b[0, 0] = 0; b[0, 1] = 1;
b[1, 0] = 2; b[1, 1] = 3;
b[2, 0] = 4; b[2, 1] = 5;
b[3, 0] = 6; b[3, 1] = 7;
b[4, 0] = 8; b[4, 1] = 9;
end example]
When an array creation expression includes both explicit dimension lengths
and an array initializer, the
lengths must be constant expressions and the number of elements at each
nesting level must match the
corresponding dimension length. [Example: Here are some examples:
int i = 3;
int[] x = new int[3] {0, 1, 2}; // OK
int[] y = new int[i] {0, 1, 2}; // Error, i not a constant
int[] z = new int[3] {0, 1, 2, 3}; // Error, length/initializer mismatch
Here, the initializer for y results in a compile-time error because the
dimension length expression is not a
constant, and the initializer for z results in a compile-time error because
the length and the number of
elements in the initializer do not agree. end example]
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【Cortex-M0】19.6 scatter文件

一个映像文件里可以包含多个域(region),它们在装载和运行时可以有不同的地址。这个地址可以用armlink的两个参数来确定: ro-base 设置代码段(RO)在装载域(load view)和运...

零基础学python-19.6 生成器函数应用

这一章节我们来讨论一下生成器函数应用。 先定义一个生成器函数: >>> def test(): for x in range(3): yield x >>> test() 从上面...

Designated Initializers在g++中不被支持

这真是个”好”消息,C和C++真心是俩门完全不同的语言,这对于我这个偶尔C,偶尔C++的程序员来说,无比痛苦。。。附Designated Initializers在C90中的用法: Standard...

Effective C#阅读笔记-8Prefer Variable Initializers to Assignment Statements

当有很多构造函数的时候,要保证变量的初始化值相同,最好的办法就是使用初始化器语法,保证每一个构造函数的变量同步。初始化器语法将会由编译器生成代码,将会在基类的构造函数和此类的构造函数之前执行,执行的顺...

C#3.0 对象与集合初始化器(Object and Collection Initializers)

对象初始化器 (Object Initializers) :.NET2.0框架中的类型非常依赖于属性。当生成对象实例和使用新的类型时,在.Net2.0时候我们像这样写:User user = new ...

ContextLoader中setContextInitializers(ApplicationContextInitializer<?>... initializers)的思考

最近查看了ContextLoader的源代码,涉及到了contextInitializerClasses参数,由于自己的项目没有用到该参数,对此产生了好奇,参考网上资料,整理一下: ContextLo...

swift - Designated Initializer & Convenience Initializers

Designated initializers tend to set all of the properties up and let the user send in values for eac...

[Python] 自动转化gcc扩展中的designated Initializers

Designated Initializers作为gcc的一种主要扩展,是代码平台移植的主要障碍之一。下面是一个针对性的例子,其它形式的designated Initializers通过修改脚本中的正...

Learning NumPy Array 无水印pdf

  • 2017-10-03 17:40
  • 3.81MB
  • 下载

LeetCode Patching Array

  • 2016-04-17 18:06
  • 452B
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)