Difference between revisions of "Array/ja"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "{{Array}} An '''array''' is a type that groups a number of variables of the same type. Examples are an array of char, an array of integer, a...")
 
Line 7: Line 7:
 
* matrices (two-dimensional array)
 
* matrices (two-dimensional array)
  
==Static Arrays==
+
==静的配列==
 
The declaration works similar to that for simple types, but you need to add the number of elements via an index range, as well as the array element type.
 
The declaration works similar to that for simple types, but you need to add the number of elements via an index range, as well as the array element type.
  
Line 21: Line 21:
 
'''startindex''' must be less than or equal to '''endindex''', and both must resolve to an integer constant, either an integer value or a [[Const|const]] value that is an integer. Either or both numbers may be negative or zero.
 
'''startindex''' must be less than or equal to '''endindex''', and both must resolve to an integer constant, either an integer value or a [[Const|const]] value that is an integer. Either or both numbers may be negative or zero.
  
===One-dimensional array===
+
===1次元配列===
 
One-dimensional array example:
 
One-dimensional array example:
  
Line 32: Line 32:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Multidimensional array===
+
===多次元配列===
 
[[Multidimensional arrays]] are supported such as [x..y,z..t] and so on.  
 
[[Multidimensional arrays]] are supported such as [x..y,z..t] and so on.  
  
Line 46: Line 46:
  
  
==Dynamic Arrays==
+
==動的配列==
 
If it is not possible to know the exact number of array elements needed at the time of the program compilation, the [[Dynamic array|dynamic array]] type can be used. A dynamic array can grow or shrink in size during program execution.
 
If it is not possible to know the exact number of array elements needed at the time of the program compilation, the [[Dynamic array|dynamic array]] type can be used. A dynamic array can grow or shrink in size during program execution.
  

Revision as of 23:15, 2 May 2017

Deutsch (de) English (en) español (es) suomi (fi) français (fr) Bahasa Indonesia (id) 日本語 (ja) русский (ru) 中文(中国大陆)‎ (zh_CN)

An array is a type that groups a number of variables of the same type. Examples are an array of char, an array of integer, and an array of real. In fact, any type, including user defined types, may be used in an array. However, the elements of an array are always of the same type. Different types cannot be grouped into an array. For this purpose, see records.

Arrays reflect the mathematical concept of

  • vectors (one-dimensional array) and
  • matrices (two-dimensional array)

静的配列

The declaration works similar to that for simple types, but you need to add the number of elements via an index range, as well as the array element type.

program
...
var 
  variablename: array [startindex..endindex] of type;
begin
  ...

startindex must be less than or equal to endindex, and both must resolve to an integer constant, either an integer value or a const value that is an integer. Either or both numbers may be negative or zero.

1次元配列

One-dimensional array example:

type
  simple_integer_array = array [1..10] of integer;
 
var
  Numbers: simple_integer_array;

多次元配列

Multidimensional arrays are supported such as [x..y,z..t] and so on.

Multidimensional array example:

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.

Element Access

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



navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring