动态数组大小在运行时在ada中确定

She*_*115 6 ada dynamic-arrays

有可能有一个大小在运行时确定的数组,如此,

Procedure prog is
   type myArray is array(Integer range <>) of Float;
   arraySize : Integer := 0;
   theArray : myArray(0..arraySize);
Begin
   -- Get Array size from user.
   put_line("How big would you like the array?");
   get(arraySize);

   For I in 0..arraySize Loop
      theArray(I) := 1.2 * I;
   End Loop;
End prog;
Run Code Online (Sandbox Code Playgroud)

除了使用动态链接列表或其他类似结构之外,有没有办法实现此结果?或者是否有一个简单的内置数据结构比使用动态链接列表更简单?

Mar*_*c C 7

当然,在块中声明如下:

procedure prog is
   arraySize : Integer := 0;
   type myArray is array(Integer range <>) of Float;
begin
   -- Get Array size from user.
   put_line("How big would you like the array?");
   get(arraySize);

   declare
      theArray : myArray(0..arraySize);
   begin
      for I in 0..arraySize Loop
         theArray(I) := 1.2 * I;
      end Loop;
   end;
end prog;
Run Code Online (Sandbox Code Playgroud)

或者将arraySize作为参数传递给子程序,并在该子程序中声明并对其进行操作:

procedure Process_Array (arraySize : Integer) is

    theArray : myArray(0..arraySize);

begin
   for I in arraySize'Range Loop
      theArray(I) := 1.2 * I;
   end Loop;
end;
Run Code Online (Sandbox Code Playgroud)

这只是说明性的(而不是编译:-),因为你需要处理诸如无效数组大小之类的事情.