Arrays
Declaration and Assignment
- Declaration of array bounds and element type
- Attributes: first, last, range, length
- Example and
Example prettified
- Bounds can be any discrete type (ie integer or enumerated range)
- Example and
Example prettified
- Bounds are fixed
- Later we will see unconstrained arrays
(ie arrays whose bounds can specified at runtime)
Aggregate Assignment
Array Types and Assignment
- Can create array types
- Can assign arrays: does element by element assignment
- Strong typing: arrays must be of same type (types must have same name)
- Example
and Example prettified
Array Declaration and Allocation
- In java, we have separate declaration and allocation
- In Ada, allocation occurs at the declaration
Arrays are NOT of Reference Type
Equality of Arrays
- Can test equality of arrays
- Does element by element comparison
- Types must have same name
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
procedure tryArraysEQ is
type MyArray is array (1..5) of Integer;
a: MyArray := (11, 12, 13, 14, 15);
b: MyArray;
type YourArray is array (1 .. 5) of Integer;
c: YourArray := (21, 22, 23, 24, 25);
begin
b := a;
if a = b then
put("Same");
else
put("Different");
end if;
a(1) := 100;
if a = b then
put("Same");
else
put("Different");
end if;
--if a = c then -- Compile error: Must be of same type
end tryArraysEQ;
-- Output:
Same
Different
MultiDimensional Arrays
- Example
type SuitType is (Clubs, Diamonds, Hearts, Spades);
Jack: constant Integer := 11;
Queen: constant Integer := 12;
King: constant Integer := 13;
Ace: constant Integer := 14;
type CardSetType is array(2 .. Ace, SuitType) of Boolean;
north, south, east, west: CardSetType;
...
north(Jack, Diamonds) := True; -- Jack of Diamonds is in North's hand
put(north'length(1));
put(north'length(2));
put(CardSetType'length(1));
put(CardSetType'length(2));
for rank in north'range(1) loop
for suit in north'range(2) loop
if north(rank, suit) then
put(rank);
put(suit);
end if;
end loop;
end loop;
- Contrast with how it would be done in java
type SuitTypeSet is array(SuitType) of Boolean;
type CardSetType is array(2 .. Ace) of SuitTypeSet;
...
north(Jack)(Diamonds)
- True multidimensional arrays (not arrays of arrays)
- Memory allocation compared with Java
Example Program
- Write a program to count all upper case letters in the standard
input and display a table of the frequency of each letter, in
alphabetical order. Only display non-zero values.
Array Slices
Array Attributes and Operations
- Attributes (can be applied to variable or type):
- 'First, 'Last, 'Range
- 'Range
- 'Length
- Operations:
- Declare type
- Declare (and allocate) variable
- Assignment (ie :=)
- Equality (ie =)
- Slice
Unconstrained Arrays
- For normal array types, the index range is fixed
- Unconstrained array types provide more flexibility:
The index range is NOT fixed
- Example:
type FloatArrayType is array(Natural range <>) of Float;
myArray: FloatArrayType(1 .. 10);
yourArray: FloatArrayType(0 .. 9);
...
-- myArray and yourArray are considered to be of the same type
-- Their types are similar to subtypes of FloatArrayType
matrix1 := matrix2;
- The range of type FloatArrayType is unconstrained
- The range must be specified when an actual variable is declared
- The bounds of the range must be Natural numbers
- The symbols <> are called a box
Strings are unconstrained arrays:
s1: String := "abc"
s2: String(1 .. 5)
s3: String(1 .. 10)
s4: String(2 .. 10)
s5: String(0 .. 10) -- Compile error
Unconstrained arrays are frequently used as formal parameters, with
constrained arrays as actual parameters
For example, Ada.Text_io.put is declared something like this:
procedure put(Item: String) is ...
Even for unconstrained arrays,
index ranges cannot change once declared (eg the size of a string
cannot change)
Unconstrained 2D Arrays
- For an unconstrained array, the number of indices and the types of the indices ARE fixed
- Example:
type MatrixType is
array(Integer range <>, Integer range <>) of Float;
-- These are like subtypes of MatrixType
matrix1: MatrixType(1 .. 10, 1 .. 5);
matrix2: MatrixType(0 .. 9, 0 .. 4);
...
-- maxtrix1 and matrix2 are considered to be of the same type
matrix1 := matrix2;
- The type declaration specifies number of indices, index types, and
element type
Declaring Array Sizes at Runtime
...
type ListType is
array(Integer range <>) of Float;
...
begin
...
get(n);
declare
list1: ListType(1 .. N);
begin
for i in list1'range loop
get(list1(i));
end loop;
for i in reverse list1'range loop
put(list1(i), 0, 1, 0);
new_line;
end loop;
end
-- list1 not defined here