Declare an array

From CodeCodex

Revision as of 19:36, 31 July 2006 by 206.160.140.2 (Talk)

Implementations

Java

One dimension

String[] oneDimension;

or

String[] oneDimension[];

or

String oneDimension[];

Populating an array with values upon declaration:

String[] oneDimension = { "a","b","c" };

Two dimension

String[][] twoDimension;

or

String[][] twoDimension[][];

or

String twoDimension[][];

Populating an array with values upon declaration:

String[][] twoDimension = { {"1","2","3"}, {"4","5","6"}, {"7","8","9"} };

OCaml

No need for type declarations in OCaml, thanks to type inference. OCaml has homogeneous arrays and provides array literals. For example:

# [|1; 2; 3|];;
- : int array = [|1; 2; 3|]

Note that array literals improve upon C-style (e.g. C++ and Java) array declarations because OCaml's array literals can be used anywhere (they are expressions) whereas the C-style is only valid on the right-hand side of an array declaration.