1

我想知道是否可以初始化这个数组:

Type Some is array (1..5) of Integer;

SomeArray : Some := ( 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 );

以某种不同的方式,像这样:

SomeArray : Some := ( others => n );

是否可以?

4

1 回答 1

3

除了“Some”现在是 Ada 2012 中的保留字之外,您可以完全按照您的描述初始化那种数组。例如:

with Text_IO; use Text_IO;

procedure Agg_Init_Test is

   type Some_Type is array (1.. 5) of Integer;

   N : constant := 4;

   Data : Some_Type := (others => N);

   procedure Init_To (N : Integer) is
   begin
      Data := (others => N);
   end Init_To;

   procedure Init_Data (Data : out Some_Type; N : Integer) is
   begin
      Data := (others => N);
   end Init_Data;

   function Inc (Val : in out Integer) return Integer is
   begin
      Val := Val + 1;
      return Val;
   end Inc;

   procedure Init_Seq(Data : out Some_Type; Start : Integer) is
      N : Integer := Start;
   begin
      Data := (others => Inc(N));
   end Init_Seq;

begin
   Init_To(42);
   Init_Data(Data, 2012);
   Init_Seq(Data, 0);
   for I of Data loop
      Put_Line(Integer'Image(I));
   end loop;
end Agg_Init_Test;
于 2012-09-24T13:31:18.447 回答