Array Initialization: Difference between revisions

<code>
(→‎STL: code tags, expanded)
(<code>)
Line 7:
=={{header|Ada}}==
The array value obtained directly from data is called array aggregate. Considering these array declarations:
<code ada>
<Ada>
type Vector is array (Integer range <>) of Integer;
type Matrix is array (Integer range <>, Integer range <>) of Integer;
type String is array (Integer range <>) of Character;
type Bits is array (Integer range <>) of Boolean;
</Adacode>
Initialization by an aggregate using positional and keyed notations:
<code ada>
<Ada>
X : Vector := (1, 4, 5);
Y : Vector (1..100) := (2|3 => 1, 5..20 => 2, others => 0);
Line 22:
L : String (1..80) := (others => ' ');
B : Bits := not (1..2 => False); -- Same as (1..2 => True)
</Adacode>
Note that the array bounds, when unconstrained as in these examples can be either determined by the aggregate, like the initialization of X shows. Or else they can be specified as a constraint, like for example in the initialization of Y. In this case '''others''' choice can be used to specify all unmentioned elements. But in any case, the compiler verifies that all array elements are initialized by the aggregate. Single dimensional arrays of characters can be initialized by character strings, as the variable S shows. Of course, array aggregates can participate in array expressions and these expressions can be used to initialize arrays. The variable B is initialized by an aggregate inversed by the operation '''not'''.
=={{header|ALGOL 68}}==
Line 51:
 
To assign a single value to an array, one uses the [] operator.
<code cpp>
// Assign the value 7 to the first element of myArray.
myArray[0] = 7;
</code>
If '''myArray''' has ten elements, one can use a loop to fill it.
<code cpp>
// Assign the sequence 1..10 to myArray
for(int i = 0; i < 10; ++i)
for(int myArray[i] = 0; i +< 110; ++i)
myArray[i] = i + 1;
</code>
If '''myArray''' has two dimensions, you can use nested loops.
<code cpp>
// Create a multiplication table
for(int y = 0; y < 10; ++y)
for(int xy = 0; xy < 10; ++xy)
for(int x = 0; myArray[x][y] =< (x+1)10; * (y+1+x);
myArray[x][y] = (x+1) * (y+1);
</code>
 
===STL===
Line 109 ⟶ 115:
Python lists are dynamically resizeable. A simple, single dimensional, array can be initialized thus:
 
<code python>
myArray = [0] * size
</pythoncode>
 
However this will not work as intended if one tries to generalize from the syntax:
 
<code python>
myArray = [[0]* width] * height] # DOES NOT WORK AS INTENDED!!!
</pythoncode>
 
This creates a list of "height" number of references to one list object ... which is a list of width instances of the number zero. Due to the differing semantics of mutables (strings, numbers) and immutables (dictionaries, lists), a change to any one of the "rows" will affect the values in all of them. Thus we need to ensure that we initialize each row with a newly generated list.
Line 123 ⟶ 129:
To initialize a list of lists one could use a pair of nested list comprehensions like so:
 
<code python>
myArray = [[0 for x in range(width)] for y in range(height)]
</pythoncode>
 
That is equivalent to:
 
<code python>
myArray = list()
for x in range(height):
myArray.append([0] * width)
</pythoncode>
973

edits