Array Types
This is a Language topic about Oxygene
Language Topics Introduction | Structured Overview | Grammar | Keywords | Functions
Oxygene supports several different kinds of arrays, of which unbound arrays are the simplest kind:
Unbound arrays
var Array1: array of String; Array2: array of Byte; begin Array1 := new String[25]; // 25 strings 0..24 Arrays2 := new Byte[40]; // 40 bytes 0..39 ...
Array Initialization
Arrays will be initialized if they comply with the three following rules:
- all bounds are specified (e.g. array[0..] of byte won't be initialized, array[0..10] of byte will be).
- they are defined as a field or local var.
- they don't have an explicitly assigned initial value.
Bound arrays
Arrays can have predetermined bounds, either in form of a fixed size (with lower and upper bound defined) or variable size (with just a lower bound specified). Starting with the May 2009 Release (3.0.19), bound arrays are allowed to have a negative lower range, that is they're allowed to be like: array[-10 .. 15] of byte.
type StringArray1 = array[0..5] of String; StringArray2 = array[1..] of String; StringArray3 = array[0.., 0..] of String; ... var array1: StringArray1; array2: StringArray2; array3: StringArray3; array4: array[0..5] of Integer; begin array1 := new StringArray; // 6 strings 0..5 array2 := new StringArray2(25); // 25 strings 1..25 array1 := new StringArray(45, 12); // 45x12 matrix of strings
In addition to Integer, chars and enums can also be used to define array bounds:
type SomeEnum = (One, Two, Three); EnumArray = array[SomeEnum.One..SomeEnum.Three] of Integer; var a: array[SomeEnum] of Integer; c: array[char] of Integer; begin a[someEnum.One] := 15; c['a'] := 15;
Inline Array Constants
You can create inline (in code) arrays without having to manually create a new array and set the elements. For example, you can write:
ListBox.Items.AddRange(['a', 'b']);
or:
x := [1,2,3]
Or
var x: array of Integer := [1,2];
Static Arrays
If you define a closed multi bound array:
type MyArray1 = array[0..10] of Byte; MyArray2 = array[0..] of Byte; MyArray3 = array[0..10, 0..10] of Byte; MyArray4 = array[0..10, 0..] of Byte;
All instances of MyArray1 and MyArray3 will be created automatically when they are used:
on the stack (local variables), or as part of a class (static or instance). Note: a static array still is a by reference type, so if you pass it as a parameter, any changes in the method that accepts the parameter will be translated back to the caller.
In other Object Pascal languages, a common code structure looks like this:
type MyArray = array[0..4096] of Integer; PMyArray = ^MyArray; begin // use the pointer PMyArray(MyPointer)[0] ...
Oxygene simplifies and allows you to do the following:
var b: ^Byte; begin b[0] = 15; b[1] := 12; end;
Inline Arrays
Inline arrays are only supported for unsafe code. See Fixed Size Buffers.
Notes
- Pointers can only be used if the project/class/method support unsafe code.
- For compatibility purposes, Oxygene does some magic under the hood so that PMyArray(MyPointer)[0] is treated the same as b[0], but there are a couple of restrictions, for example the array must be single dimensional and start at 0.