Array Types
Elements knows three different array types, not all of which are available on all languages or on all platforms.
Static and Dynamic Arrays
Static and Dynamic Arrays are low-level language-based arrays that are not represented via classes or records, but map directly to memory space containing the elements (or pointers to the elements).
For the static variant of these basic arrays, a fixed size (in one or multiple dimensions) is provided at compile time, and the array is allocated directly on the stack of the containing method, or as part of the memory layout of the object. For example, for a static array of eight Int32s, 32 (8 times 4) raw bytes would be reserved to hold those eight integer values.
Static arrays are comparable to language-level arrays in C/Objective-C or Delphi.
var x: array [0..10] of String;
var x: array [0..10, 0..10] of String; // 10x10
var x: String[10]
var x: String[10,10] // 10x10
Dynamic arrays are pretty similar to static ones, but memory for them is allocated at runtime on the heap (often for a size that is not known at compile time). Like with static arrays, a flat memory space of the size needed to contain all elements is allocated, in addition to some extra overhead for keeping track of the array reference count, under ARC.
var x: array of String; // this is nil
var x: array of String := new String[10];
var x: array [0..] of String; // this is nil
var x: array [0..] of String := new String[10];
var x: array [0.., 0..] of String; // this is nil
var x: array [0.., 0..] of String := new String[10,10];
var x: array of array of String; // this is nil
var x: array of array of String := ... // each row needs to be initialized manually
The first two array syntaxes (for single-dimension arrays) are considered identical.
String[] x; // this is null
String[] x = new String[10];
String[,] x; // this is null
String[,] x = new String[10,10];
String[] x; // this is null
String[][] x = ... // each row needs to be initialized manually
var x: String[]? // this is nil (and hence needs to be optional)
var x: String[] = String[](10)
var x: String[][]? // this is nil
var x: String[][] = ... // each row needs to be initialized manually
String[] x; // this is null
String[] x = new String[10];
The Java language has no syntax for multi-dimensional arrays.
High-Level Arrays
Available in Swift only, high-level arrays provide higher-level and more object-oriented access to an array-like data structure, with the exact implementation being encapsulated by the compiler. These are comparable to using a List<T>
class (.NET, Java and Island) or NS(Mutable)Array
(on Cocoa).
var x: [String]? // this is nil (and hence needs to be optional)
var x: [String] = [String](count: 10, repeatedValue: "")
Syntax Examples:
Description | Oxygene | C# & Java | Swift |
---|---|---|---|
Static Array | var x: array [0..5] of String | N/A | var x: String[5] |
Dynamic Array var | var x: array of String := ... | String[] x = ... | var x: String[] = ... |
Dynamic Array .ctor | x := new String[5] | x = new String[5] | x = String[](count: 5) |
High-level Array var | N/A | N/A | var x = [String] |
High-level Array .ctor | N/A | N/A | var x = [String](count: 5; repeatedValue: "") |
Type Mapping
- On .NET, static and dynamic arrays map to the
System.Array
FCL type. - On Cocoa, Java and Island, static and dynamic arrays map to the [Todo: add info].
- On all platforms, high-level arrays leverage the the
Swift.Array<T>
struct defined in the open source Swift Base Library.
In addition to per-platform collection classes that can be used instead of or in addition to arrays, there is also a RemObjects.Elements.RTL.List<T>
type defined by Elements RTL for use in code that is shared across platforms. It is very similar in concept to Swift.Array<T>
.
Availability by Language
- High-level arrays (
[T]
) as language feature are only available in Swift. - Static arrays are only available in Oxygene and Swift, but not when using C# or the Java Language.
- Multi-dimensional arrays are not supported by the Java Language.
See Also
- Array Types in Oxygene
Swift.Array<T>
Struct in the Swift Base Library