[ Table of Contents ]
An array type declaration specifies an array of components, all having the same type. An array can have one or more dimensions. Each dimension is associated with an index value whose type is either Integer or an enumeration type. The array dimensions may be either constrained or unconstrained. (All objects of a constrained array type have the same size.) The part of the declaration that specifies the component type may include a range constraint.
Constrained Array Type Declarations |
||||||
|
Declarations of unconstrained arrays include the symbol "<>" often pronounced as "box" and refereed to as box notation. An unconstrained array type is known as an indefinite subtype. When an actual object is created, it must be given definite form with an initial value or constraint.
Unconstrained Array Type Declarations |
||||
|
It is legal, and often convenient, to declare a one-of-a-kind array without having previously declared an appropriate array type. Such an array is said to be of an anonymous type. An initialization part, including an aggregate expression, may be included as part of the declaration.
One-of-a-kind Array Declarations |
||||
|
The ((1..4) => 0.0) is an example of an aggregate expression used during initialization. (An example appeared in Chapter 3 in the illustration of loop statements.)
Attributes of Array Types and Arrays
There are four attributes that apply to constrained array types or to array objects. They are: A'Range(N), A'First(N), A'Last(N) and A'Length(N) where N represents one of the dimensions. If the (N) part is omitted, the first dimension is implied -- often used in the case of one-dimensional arrays.
Example Program Illustrating Array Types and Attributes
This program depends upon Ada.Text_IO (from the predefined environment)
and Sluggers (reused from the previous example). The Slugger_Arrays package declaration exports one procedure while its body declares (encapsulates) several types, several arrays and an instance of the generic package Fixed_IO. The arrays contain data for five major league baseball players who played in the 1998 season: Belle, Griffey, McGuire, Sosa and Vaughn. |
Note the explicit conversions from Integer to Float and from Integer to ABPHR_Fixed_Type.
Source Code Listing
-------------------------------------------------------------- ------------------------- Slugger_Arrays --------------------- -- This package exports a single procedure that displays data -- about home run sluggers. Its body declares three types, -- including two array types, and three arrays. The arrays -- contain data for five sluggers in the 1998 season. An -- instance of the the generic package, Fixed_IO, is also -- declared. -------------------------------------------------------------- package Slugger_Arrays is procedure Display_Slugger_Data; end Slugger_Arrays; -------------------------------------------------------------- with Sluggers; with Text_IO; use Text_IO; package body Slugger_Arrays is -- types type Batting_Int_Type is range 0..999; type Batting_Data_Type is array (1..5) of Batting_Int_Type; type String_12 is array (1..12) of Character; -- arrays Name : array(1..5) of String_12 := ("Albert Belle","Ken Griffey ", "Mark McGuire","Sammy Sosa ","Greg Vaughn "); At_Bats : Batting_Data_Type := (609,633,509,643,573); Home_Runs : Batting_Data_Type := ( 49, 56, 70, 66, 50); package FXIO is new Fixed_IO(Sluggers.ABPHR_Fixed_Type); ---------------------------------------- procedure Display_Slugger_Data is ABPHR_FL : Sluggers.ABPHR_Float_Type; ABPHR_FX : Sluggers.ABPHR_Fixed_Type; begin Put_Line(" Sluggers of 1998"); Put_Line(" Name At Bats Home Runs AB/HR"); New_line; for I in 1..5 loop ABPHR_FL := Sluggers.ABPHR(Float(At_Bats(I)), -- 2 conversions Float(Home_Runs(I))); ABPHR_FX := Sluggers.ABPHR_Fixed_Type(ABPHR_FL); -- 1 conversion Put(String(Name(I)) & " "); Put(Batting_Int_Type'Image(At_Bats(I))); Put(" "); Put(Batting_Int_Type'Image(Home_Runs(I))); Put(" "); FXIO.Put(ABPHR_FX); New_Line; end loop; end Display_Slugger_Data; ------------------------------------------- end Slugger_Arrays; -------------------------------------------------------------- -------------------------- Test_Arrays ----------------------- -- This test procedure calls the procedure that displays -- the slugger data. -------------------------------------------------------------- with Slugger_Arrays; procedure Test_Arrays is begin Slugger_Arrays.Display_Slugger_Data; end Test_Arrays; -------------------------------------------------------------- |
The above program produces the following output:
Sluggers of 1998 Name At Bats Home Runs AB/HR Albert Belle 609 49 12.4 Ken Griffey 633 56 11.4 Mark McGuire 509 70 7.3 Sammy Sosa 643 66 9.7 Greg Vaughn 573 50 11.5 |
Array Slices
A slice is a contiguous portion of a one-dimensional array, denoted using an index range. For example, the elements:
"Mark McGuire", "Sammy Sosa "
in the above program would correspond to Name(3..4), a slice of the full Name array.
Related Topics
[ Back to top of page ]