with Ada.Text_IO; use Ada.Text_IO; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; procedure Arrays is ----------------------------------------------------------------------------- -- Arrays 101. -- -- The index is strongly-typed and can be any discrete type. -- -- Iterations over the index type are preferred over iterations over specific -- index values. ----------------------------------------------------------------------------- procedure Test_Array is type My_Int is range 0 .. 1_000; type Index is range 1 .. 5; type My_Int_Array is array (Index) of My_Int; function To_String (A : My_Int_Array) return String is S : Unbounded_String; begin for I in Index loop S := S & My_Int'Image (A (I)) & " "; end loop; return To_String (S); end To_String; Arr : My_Int_Array := (2, 3, 5, 7, 11); -- This array is not actually empty; its size is determined by the range of -- its index type, which in this example is 1 .. 5. Empty_Arr : My_Int_Array; begin Put_Line ("Arr = " & To_String (Arr)); Put_Line ("Arr is " & Integer'Image (Arr'Size) & " bytes"); Put_Line ("Empty_Arr = " & To_String (Empty_Arr)); Put_Line ("Empty_Arr is " & Integer'Image (Empty_Arr'Size) & " bytes"); end Test_Array; ----------------------------------------------------------------------------- -- Enums as array indices. ----------------------------------------------------------------------------- procedure Test_Enum_Array is type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); type Day is range 1 .. 31; Month_Days : array (Month) of Day := (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); begin for M in Month loop Put_Line (Month'Image (M) & " has " & Day'Image (Month_Days (M)) & " days"); end loop; end Test_Enum_Array; ----------------------------------------------------------------------------- -- Arrays in ADA are bounds-checked. ----------------------------------------------------------------------------- procedure Test_Bounds is Arr : array (Integer range 1 .. 5) of Integer := (3, 4, 7, 8, 9); begin Arr (1) := 17; Arr (5) := 18; --Arr (6) := 19; -- Error. end Test_Bounds; ----------------------------------------------------------------------------- -- Use the Range attribute to iterate over an array with an anonymous range. ----------------------------------------------------------------------------- procedure Test_Anonymous_Range is Arr : array (3 .. 7) of Integer := (5, 8, 3, 5, 3); begin for I in Arr'Range loop Put_Line ("Index " & Integer'Image (I) & " has value " & Integer'Image (Arr (I))); end loop; end Test_Anonymous_Range; ----------------------------------------------------------------------------- -- Unconstrained arrays. -- -- The size/bounds are provided when creating an instance of the array type. ----------------------------------------------------------------------------- procedure Test_Unbounded_Array is type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); type Days_Arr is array (Integer range <>) of Day; Days_Off : Days_Arr := (Sat, Sun); begin Put ("Holidays: "); for D in Days_Off'Range loop Put (Day'Image (Days_Off (D)) & " "); end loop; New_Line; end Test_Unbounded_Array; ----------------------------------------------------------------------------- -- Bounds are automatically inferred from the initialization value. ----------------------------------------------------------------------------- procedure Test_Auto_Bounds is Arr : array (Natural range <>) of Integer := (2, 3, 4); begin for I in Arr'First .. Arr'Last loop Put_Line ("Arr(" & Integer'Image (I) & ") = " & Integer'Image (Arr (I))); end loop; end Test_Auto_Bounds; ----------------------------------------------------------------------------- -- Array slices. ----------------------------------------------------------------------------- procedure Test_Slices is Str : String := "Hello world"; begin Str (7 .. 11) := "there"; Put_Line (Str); end Test_Slices; begin Test_Array; Test_Enum_Array; Test_Bounds; Test_Anonymous_Range; Test_Unbounded_Array; Test_Auto_Bounds; Test_Slices; end Arrays;