| Author |
Message |
chuckbeginner

Joined: May 26, 2008 Posts: 29 Location: Asheville NC
|
Posted: Sun Nov 16, 2008 10:31 am Post subject:
modifying array sizes Subject description: is there a way to do it? |
 |
|
I would like to change the size of the array after I have already filled the entries of the array, then throw data into the new empty slots. Is this possible?
I have been trying
array.cap() => x;
x+1 => array.cap;
When I do <<<array>>>, it gives me the right answer. But when I try to assign a value to the slot I think I just created , it gives me an ArrayOutOfBounds message.
Is this possible? Do any of you know the correct syntax/method? |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sun Nov 16, 2008 12:43 pm Post subject:
|
 |
|
Sure.
From /examples/array/dynamic.ck;
| Code: | // instantiate int array
float argh[0];
// print out relevant info
<<< "array size:", argh.size() >>>;
// append items (array should grow dynamically as needed)
argh << 3.0 << 4 << 5;
// print out relevant info
<<< "array size:", argh.size() >>>;
// print out elements
<<< "contents:", argh[0], argh[1], argh[2] >>>;
// pop element
argh.popBack();
// print out relevant info
<<< "array size:", argh.size() >>>; |
The "<<" operator appends a array with a element. That is; it increases the size by one and sets this value/object as the last one.
Also useful; | Code: |
int array[3];
<<<"size =", array.cap()>>>;
(array.cap() + 1) => array.size;
<<<"now size =", array.cap()>>>;
12 => array[3];
<<<"printing array's contents", "">>>;
for(int n; n< array.cap(); n++) <<<array[n]>>>; |
Writing to .cap used to work (though at the expense of flushing the data) but it seems it doesn't any more; .size() is more useful now.
Not that at the moment there is a bug for arrays of non-primitive types that causes the elements to be initiated non-properly if .size() is used and you should use this instead;
| Code: |
my_array << new my_object; |
Hopefully this will soon be fixed. _________________ Kassen |
|
|
Back to top
|
|
 |
chuckbeginner

Joined: May 26, 2008 Posts: 29 Location: Asheville NC
|
Posted: Mon Nov 17, 2008 1:45 pm Post subject:
|
 |
|
Thanks for the help!
What version was this functionality implemented in? I don't see that example file in my examples folder... |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Mon Nov 17, 2008 6:00 pm Post subject:
|
 |
|
Hmmmm, I think that's 1.2.1.2 so the latest. I unless you have good reasons not to I'd stick to using the latest version and using the list(s) to make sure you are notified when one gets released. _________________ Kassen |
|
|
Back to top
|
|
 |
|