如何使用类型在 Pascal 中初始化数组?

发布于 2025-01-20 11:31:17 字数 669 浏览 5 评论 0原文

为了好玩,我想刷新我的 Pascal 知识,所以必须再次复习一下基础知识。

让我们看看这段 Java 代码:

class ArrayTest {

    public static void main(String args[]){
        int[] numArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    }
}

将其重写为 Pascal:

type
    arrNum = array[1..10] of integer;
var
    BBB: arrNum;

begin
    BBB := [1,2,3,4,5,6,7,8,8,10];
end.

在 MacOS 上使用 FPC 3.2.2 编译代码,结果是:

arrnum.pas(7,12) 错误:不兼容类型:得到“{Array Of Const/Constant Open} Array > of ShortInt”,预期为“arrNum”

arrnum.pas(10) 致命:编译模块时出现 1 个错误,正在停止

致命:编译中止错误:/usr/local/bin/ppcx64 返回一个 错误退出代码

这里出了什么问题?

For the sake of fun, I'd like to refresh my Pascal knowledge, so gotta review the basics again.

Let's see this Java code:

class ArrayTest {

    public static void main(String args[]){
        int[] numArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    }
}

Rewrite that into Pascal:

type
    arrNum = array[1..10] of integer;
var
    BBB: arrNum;

begin
    BBB := [1,2,3,4,5,6,7,8,8,10];
end.

Compiling the code with FPC 3.2.2 on MacOS, the result is:

arrnum.pas(7,12) Error: Incompatible types: got "{Array Of Const/Constant Open} Array > of ShortInt" expected "arrNum"

arrnum.pas(10) Fatal: There were 1 errors compiling module, stopping

Fatal: Compilation aborted Error: /usr/local/bin/ppcx64 returned an
error exitcode

What's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光病人 2025-01-27 11:31:17

使用构造函数的类似语法可用于动态阵列(BBB),但不能用于静态阵列。可以通过将类型常数分配给变量(BB10:= CCC10)或定义初始化常数(DDD10)来完成静态初始化。

以下代码证明了某些情况:

{$mode delphi} 
type
    arrNum = array of integer;
    arrNum10 = array[0..9] of integer;

const CCC10 : arrNum10 = (1,2,3,4,5,6,7,8,9,10);
var
    BBB: arrNum;
    BBB10 : arrNum10;


    DDD10  : arrnum10 = (1,2,3,4,5,6,7,8,9,10);

begin
    BBB := ArrNum.Create(1,2,3,4,5,6,7,8,8,10);
    BBB10:=CCC10;
end.

A similar syntax using a constructor is available for dynamic arrays (BBB), but not for static ones. Static initializations can be done by assigning a type constant to a variable (BB10:=CCC10) or by defining an initialized constant(DDD10).

The below code demonstrates some cases:

{$mode delphi} 
type
    arrNum = array of integer;
    arrNum10 = array[0..9] of integer;

const CCC10 : arrNum10 = (1,2,3,4,5,6,7,8,9,10);
var
    BBB: arrNum;
    BBB10 : arrNum10;


    DDD10  : arrnum10 = (1,2,3,4,5,6,7,8,9,10);

begin
    BBB := ArrNum.Create(1,2,3,4,5,6,7,8,8,10);
    BBB10:=CCC10;
end.
鹤仙姿 2025-01-27 11:31:17

您可以在声明中初始化它:

program ideone;
type
    arrNum = array[1..10] of integer;
var
    BBB: arrNum = (1,2,3,4,5,6,7,8,8,10);
    i: Integer;
begin
    for i := 1 to 10 do
        Write(BBB[i])
end.

You can initialize it in the declaration:

program ideone;
type
    arrNum = array[1..10] of integer;
var
    BBB: arrNum = (1,2,3,4,5,6,7,8,8,10);
    i: Integer;
begin
    for i := 1 to 10 do
        Write(BBB[i])
end.
人间☆小暴躁 2025-01-27 11:31:17

在pascal [1,  2,  3,4,  5,6,6,  7,8,8,  8,8,  10] set字面包含9个不同的整数值。
您无法将设置分配给array数据类型变量。

取而代之的是,您需要指定array字面的且正确表示它,

arrNum[1: 1; 2: 2; 3: 3; 4: 4; 5: 5; 6: 6; 7: 7; 8: 8; 9: 8; 10: 10]

但是,如下所示,FPC(尚未)不符合扩展的Pascal规范(ISO Standard 10206)。 。
他们酿造了自己的pascal,因此编译器(“ mis-”)将您的set字面标识为“ const/const/constant open open} arnay and and and and and and and and and and and and and and and and and shortint”。
参见 marco的答案如何初始化“ ”作为替代方案。

In Pascal [1, 2, 3, 4, 5, 6, 7, 8, 8, 10] is a set literal containing 9 distinct integer values.
You cannot assign sets to an array data type variable.

Instead you will need to specify an array literal and properly denote it, like so:

arrNum[1: 1; 2: 2; 3: 3; 4: 4; 5: 5; 6: 6; 7: 7; 8: 8; 9: 8; 10: 10]

However, as of version 3.2.0 the FPC does not (yet) comply with the Extended Pascal specification (ISO standard 10206).
They brewed up their own Pascal, so the compiler (“mis‑”)identifies your set literal as an “{Array Of Const/Constant Open} Array of ShortInt”.
See Marco’s answer how to initialize “dynamic arrays” as an alternative.

心安伴我暖 2025-01-27 11:31:17

如果声明为数组,则应该使用 for 循环来初始化

type
    arrNum = array [ 1..10] of integer;
var
    BBB: arrNum;
    i: integer;

begin
    for i:=1 to 10 do
    BBB[i] := i;
end.

If you declare as an array you should use a for loop to init

type
    arrNum = array [ 1..10] of integer;
var
    BBB: arrNum;
    i: integer;

begin
    for i:=1 to 10 do
    BBB[i] := i;
end.
千鲤 2025-01-27 11:31:17

您可以使用 for 循环初始化它

对于 i := 1 到 10 做
BBB[i]:=i;

或者

对于 i := 1 到 10 做
BBB[i]:=0;

You can initialize it using for loop

for i := 1 to 10 do
BBB[i]:=i;

or

for i := 1 to 10 do
BBB[i]:=0;

拒绝两难 2025-01-27 11:31:17

使用动态数组和Delphi兼容模式,可以使用此语法分配数组:

{$mode delphi}

type
  arrNum = TArray<integer>;
var
  BBB: arrNum;    
begin
  BBB := [1,2,3,4,5,6,7,8,8,10];
end.

如果您需要一个静态常数(动态)数组,则分配相似:

const
  BBB : arrNum = [1,2,3,4,5,6,7,8,8,10];

Using a dynamic array and delphi compatibility mode, it is possible to assign an array using this syntax:

{$mode delphi}

type
  arrNum = TArray<integer>;
var
  BBB: arrNum;    
begin
  BBB := [1,2,3,4,5,6,7,8,8,10];
end.

If you want a static constant (dynamic) array, the assignment is similar:

const
  BBB : arrNum = [1,2,3,4,5,6,7,8,8,10];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文