Array Initialization: Difference between revisions
Content added Content deleted
(add E examples) |
|||
Line 252: | Line 252: | ||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. |
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. |
||
$ |
$ |
||
=={{header|Groovy}}== |
|||
Follows the pattern of the fascinating Fortran example above: |
|||
Arrays and lists are synonymous in Groovy. They can be initialized with a wide range of operations and Groovy enhancements to the Collection and List classes. |
|||
<lang groovy>def aa = [ 1, 25, 31, -3 ] // list |
|||
def a = [0] * 100 // list of 100 zeroes |
|||
def b = 1..9 // range notation |
|||
def c = (1..10).collect { 2.0**it } // each output element is 2**(corresponding invoking list element) |
|||
// There are no true "multi-dimensional" arrays in Groovy (as in most C-derived languages). |
|||
// Use lists of lists as a stand in. |
|||
def d = (0..1).collect { i -> (1..5).collect { j -> 2**(5*i+j) as double } } |
|||
def e = [ [ 1.0, 2.0, 3.0, 4.0 ], |
|||
[ 5.0, 6.0, 7.0, 8.0 ], |
|||
[ 9.0, 10.0, 11.0, 12.0 ], |
|||
[ 13.0, 14.0, 15.0, 16.0 ] ] |
|||
println aa |
|||
println b |
|||
println c |
|||
println() |
|||
d.each { print "["; it.each { elt -> printf "%7.1f ", elt }; println "]" } |
|||
println() |
|||
e.each { print "["; it.each { elt -> printf "%7.1f ", elt }; println "]" }</lang> |
|||
Output: |
|||
<pre>[1, 25, 31, -3] |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9] |
|||
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] |
|||
[ 2.0 4.0 8.0 16.0 32.0 ] |
|||
[ 64.0 128.0 256.0 512.0 1024.0 ] |
|||
[ 1.0 2.0 3.0 4.0 ] |
|||
[ 5.0 6.0 7.0 8.0 ] |
|||
[ 9.0 10.0 11.0 12.0 ] |
|||
[ 13.0 14.0 15.0 16.0 ]</pre> |
|||
Here is a more interesting example showing a function that creates and returns a square identity matrix of order N: |
|||
<lang groovy>def identity = { n -> |
|||
(1..n).collect { i -> (1..n).collect { j -> i==j ? 1.0 : 0.0 } } |
|||
}</lang> |
|||
Test program: |
|||
<lang groovy>def i2 = identity(2) |
|||
def i15 = identity(15) |
|||
i2.each { print "["; it.each { elt -> printf "%4.1f ", elt }; println "]" } |
|||
println() |
|||
i15.each { print "["; it.each { elt -> printf "%4.1f ", elt }; println "]" }</lang> |
|||
Output: |
|||
<pre>[ 1.0 0.0 ] |
|||
[ 0.0 1.0 ] |
|||
[ 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 ] |
|||
[ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 ] |
|||
[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