Arrays
Arrays
Arrays are sets of similar objects ordered by index.
Think about vectors in physics, or a game board such as chess.
All positions on the board are ordered 1-8 and a-h, and only contain chess pieces (or absent piece). It should never contain cheese.
Same way, an array can contain only objects of the same type.
In fact, we can easily create a chess board so easily with arrays!
package Chess is
type position is (
empty, pawn, tower, knight, bishop, queen, king
);
type Board_Type
is array(1..8,'A'..'H') -- indexes
of Position -- Types of elements
:= (others => empty) ; --Initialization of unspecified
Board : Board_Type;
begin
Board(1,'E'):= king;
...
end Chess;
Anonymous Arrays
The proper way to do it is to create a type, but sometimes you are only going to use a certain array once, so you can use an array that makes a type just for it. Bad news is it can't interact with none other array.
Vector : array (1..3) of Float;
Matrix : array (1..3, 1..2)
of Natural := ((1,2),
(3,1),
(2,3));
Quat: array (1..2, 1..3, 1..2)
of Natural := (( (1,2),(3,1),(2,3)),
( (1,2),(3,1),(2,3))
);
Indexes
As it should be natural, indexes can be of any type and start at any value. Nonetheless, they should be specified when creating the array.
As index establishes the size, there are many operations that are size or index dependent.
V1 : array (1..3) of Integer;
V2 : array (0..2) of Integer;
V3 : array (-1..1) of Integer
--Same sizes, different index
...
V1 := (2,4,8); --Positional assignation
V2 := ( 1 => 3, 0 => 1, 2 => 9);
V3 := ( 0 => 0, others => 1);
--Named assignation index => value