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]

Array constants can only be used in initializers

if you use the {...} array form in anything but an initializer.  WRONGprivate int[] foo() { int[...
  • quechao123
  • quechao123
  • 2010年06月19日 16:39
  • 2032

定义了一个数组 int CC [] ={1,2,3}; Array constants can only be used in initializers.

只能在声明时这样赋值int CC [] ={1,2,3}; 声明后就用循环赋值或者单个赋值。 for(i=0; i...
  • u010408365
  • u010408365
  • 2014年10月24日 13:20
  • 1087

class viewController has no initializers解决办法

IOS开发的时候,有时候会出现如下的错误: class viewController has no initializers! 解决的办法: 将UIViewControlle...
  • tianmaxingkong_
  • tianmaxingkong_
  • 2016年01月26日 16:53
  • 5890

class viewcontroller has no initializers 如何fixed

import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ...
  • lcg910978041
  • lcg910978041
  • 2016年06月15日 16:52
  • 1014

Class has no initializers: Swift Error

var searchController: UISearchController Change it tovar searchController: UISearchController!
  • Xoxo_x
  • Xoxo_x
  • 2016年10月31日 15:57
  • 903

Designated Initializers在g++中不被支持

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

Kubernetes 新概念 “Initializers”解析(上):能让你为集群编写插件的新模型

Kubernetes v1.7 新增了 Initializers,它可以用来方便地扩展准入控制,今天的文章来自 Google Kubernetes 现役工程师 Ahmet Alp Balkan,让他带...
  • qq_34463875
  • qq_34463875
  • 2017年09月27日 15:29
  • 115

sorry, unimplemented: non-trivial designated initializers not supported解决

这是因为C和C++结构体初始化不同造成的 对于一个结构体 C里面初始化static const AVClass framesync_class = { .version ...
  • zuicong5568
  • zuicong5568
  • 2017年09月12日 14:06
  • 135

iOS 开发 -- Swift (九) 构造函数

构造函数基础 构造函数是一种特殊的函数,主要用来在创建对象时初始化对象,为对象成员变量设置初始值,在 OC 中的构造函数是 initWithXXX,在 Swift 中由于支持函数重载,所有的构造函数...
  • zhoutaifeng
  • zhoutaifeng
  • 2016年02月26日 15:55
  • 165

Spring 初始化过程跟踪

1,在web.xml中有             spring监听器         org.springframework.web.context.ContextLoaderListener...
  • YONGHUMING6342
  • YONGHUMING6342
  • 2016年03月22日 11:37
  • 654
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:19.6 Array initializers
举报原因:
原因补充:

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