|
@@ -1,32 +1,35 @@
|
|
-Dynamic arrays are introduced because oberon has no option for dynamically grown sequences except linked lists (and linked lists are not efficient comparing to arrays in some cases).
|
|
|
|
|
|
+Dynamic arrays are similar to static arrays but their length can be changed at runtime - new elements can be added or removed.
|
|
|
|
|
|
### Syntax
|
|
### Syntax
|
|
-Dynamic arrays are declared in the same way as oberon arrays - 'dynamic' dimension is marked with '*':
|
|
|
|
|
|
+Dynamic arrays are declared in the same way as static arrays - 'dynamic' dimension is marked with '*':
|
|
|
|
|
|
a1: ARRAY * OF INTEGER;
|
|
a1: ARRAY * OF INTEGER;
|
|
a2: ARRAY *, * OF BOOLEAN;
|
|
a2: ARRAY *, * OF BOOLEAN;
|
|
a3: ARRAY *, 10, * OF CHAR;
|
|
a3: ARRAY *, 10, * OF CHAR;
|
|
|
|
|
|
### Semantics
|
|
### Semantics
|
|
-Dynamic arrays are similar to static arrays but their length can be changed at runtime. Initial length is 0. To add a new element *add* method is used:
|
|
|
|
|
|
+Initial length of dynamic array is 0.
|
|
|
|
+To add a new element *add* method is used:
|
|
|
|
|
|
array.add(value);
|
|
array.add(value);
|
|
|
|
|
|
-Added *value* type should be compatible with the array elements type.
|
|
|
|
|
|
+Added _value_ type should be compatible with the array elements type.
|
|
To remove element from array *remove* method is used:
|
|
To remove element from array *remove* method is used:
|
|
|
|
|
|
array.remove(index);
|
|
array.remove(index);
|
|
|
|
|
|
-INTEGER *index* specifies element index to remove.
|
|
|
|
|
|
+INTEGER _index_ specifies element index to remove.
|
|
Method *clear* is used to remove all elements from array:
|
|
Method *clear* is used to remove all elements from array:
|
|
|
|
|
|
array.clear();
|
|
array.clear();
|
|
|
|
|
|
Dynamic arrays also have [[indexOf|eberon-array-methods]] method (similar to static arrays).
|
|
Dynamic arrays also have [[indexOf|eberon-array-methods]] method (similar to static arrays).
|
|
|
|
|
|
-Dynamic array can be passed as open array to procedure.
|
|
|
|
-Procedure cannot have dynamic array as non-VAR parameter.
|
|
|
|
-Procedure can have dynamic array as a result - in this case a copy of array is returned.
|
|
|
|
|
|
+* Dynamic array can be passed as open array to procedure.
|
|
|
|
+* Procedure can have dynamic array as VAR parameter and change content of passed array. But dynamic array cannot be passed as non-VAR paramater - ordinary open array should be used.
|
|
|
|
+* Procedure can have dynamic array as a result - in this case a copy of array is returned.
|
|
|
|
+
|
|
|
|
+Example:
|
|
|
|
|
|
MODULE Test;
|
|
MODULE Test;
|
|
TYPE
|
|
TYPE
|
|
@@ -48,5 +51,7 @@ Procedure can have dynamic array as a result - in this case a copy of array is r
|
|
ASSERT(a2[0] = 3); (*array was copied, a1 and a2 do not share elements*)
|
|
ASSERT(a2[0] = 3); (*array was copied, a1 and a2 do not share elements*)
|
|
END Test.
|
|
END Test.
|
|
|
|
|
|
-Dynamic array can be assigned to open, static or another dynamic array but not vice versa. Element types should be compatible in this case.
|
|
|
|
-Dynamic array cannot be assigned to NIL.
|
|
|
|
|
|
+* Dynamic array can be assigned to another dynamic array - destination array content is replaced by the content of source array, lenght became the same as the length of source array.
|
|
|
|
+* Dynamic array can be assigned to open or static array but not vice versa.
|
|
|
|
+* Element types should be compatible for all array assignment operations.
|
|
|
|
+* Dynamic array cannot be assigned to NIL.
|