How to define the size of an array during runtime?
How to define the size of an array during runtime?
I would like to create an ARRAY and define its size during runtime instead of compilation time. Why? This year I have done a Java course. After that course I translated class ArrayList from the library java.util.ArrayList to Component Pascal. This library automatically extend its size of the array automatically if it is to small. It is done by creating a new ARRAY of bigger size and copy the old ARRAY to the new one. The old array is deleted after the expansion.
-
- Posts: 55
- Joined: Wed Sep 18, 2013 10:06 pm
- Contact:
Re: How to define the size of an array during runtime?
For many years the Gardens Point implementation of Component Pascal has had a language extension for extensible array types. These are called vectors:
The source code is available on the GitHub gpcp site.
For more details read section 4.6 Extensible arrays: the vector types in the GPCP Release Notes.Values of these vector types are dynamically allocated, and automatically extend their capacity when an append operation is performed on an array that is already full. Vectors may be declared to have any element type, and extend their length using amortized doubling. In most circumstances when a linked list would otherwise have been used the vector
types are faster, more memory efficient, and allow memory-safe indexing. Elements of vectors may be accessed using the familiar index syntax, with index values check
The source code is available on the GitHub gpcp site.
-
- Posts: 263
- Joined: Tue Sep 17, 2013 6:50 am
Re: How to define the size of an array during runtime?
you need to use a dynamic array, i.e. a POINTER TO ARRAY. Then you can specify the size with NEW.
In Java it is exactly the same but the syntax differs slightly
Josef
In Java it is exactly the same but the syntax differs slightly
Josef
Re: How to define the size of an array during runtime?
Josef thank you very much for the hint. That is the solution of my problem.
Also thank you to Chris. I'll look on your solution later. I think Josef's way is more elegant.
- Helmut
Also thank you to Chris. I'll look on your solution later. I think Josef's way is more elegant.
- Helmut