Array/ja
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
Bahasa Indonesia (id) │
日本語 (ja) │
русский (ru) │
中文(中国大陆) (zh_CN) │
配列 は、同じ種類の 変数 の集合です。 例えば、 char や、 integer や, real の配列です。 実際、ユーザー定義型を含むどのような種類の変数も、配列の形に扱うことができます。 しかしながら、 配列の要素は同じ種類でなくてはいけません。 つまり、異なる種類の変数は、ひとつの配列の中に集めることはできません。 この目的のためには、 record を用いてください。
配列は、数学的には以下の反映となります。
- ベクトル (1次元配列)
- マトリックス (2次元配列)
静的配列
宣言部は単純な種類と似ていますが、配列の要素の種類と、インデックスの範囲を指定して要素数を設定する必要があります。
program
...
var
variablename: array [startindex..endindex] of type;
begin
...
startindex は endindex と等しいか、それより小さい必要があります。 そしていずれも Integer 定数( Integer、または const)である必要があります。 また、片方または両方の値がマイナスまたは0(ゼロ)の値を持つことができます。
1次元配列
1次元配列の参考例を以下に示します。:
type
simple_integer_array = array [1..10] of integer;
var
Numbers: simple_integer_array;
多次元配列
多次元配列 は、 [x..y,z..t] などの表記をします。
多次元配列の参考例を以下に示します。:
type
more_complex_array = array [0..5,1..3] of extended;
var
specialmatrix: more_complex_array;
動的配列
If it is not possible to know the exact number of array elements needed at the time of the program compilation, the dynamic array type can be used. A dynamic array can grow or shrink in size during program execution.
配列要素へのアクセス
To access an array element you need to include the element position between brackets ([]) along with the name of the array variable. The element can then be used like a simple variable. But if you want to use parameters you MUST use a structure because else it will cause errors or bugs... (I do not understand, what is meant here).
Var
my_array : array[1..3] of Integer;
my_matrix : array[1..5,1..5] of Integer;
some_value : Integer;
...
begin
my_array[2] := a + 2;
my_matrix[2,3] := some_value;
...
some_value := my_array[2];
some_value := my_matrix[4,3];
end.
Array Literals
There are two formats used for array literals, depending on where they are placed. In the variable declaration section, you can initialize static arrays (it is not possible with dynamic arrays) with a series of values placed inside parentheses. In a statement block you can create an anonymous array with a series of values inside of brackets. For example:
Var
// initialize static integer array via array literal
Numbers : array [1..3] of Integer = (1, 2, 3);
procedure PrintArray(input : array of String);
var
i : integer;
begin
for i := 1 to length(input) do
write(input[i - 1],' ');
writeln;
end;
begin
Writeln( Numbers[2] );
// create three item anonymous string array via an array literal
PrintArray( ['one', 'two', 'three'] );
end.
Output:
2
one two three
simple data types |
|
---|---|
complex data types |