15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

320<br />

Working with Resizable Arrays<br />

Dim BigArray(5) as String<br />

Figure 1-6:<br />

Resizing an<br />

array lets<br />

you expand<br />

or shrink an<br />

array.<br />

0 1<br />

2<br />

Jan Howards Mike Edwards John Perkins<br />

0 1 2<br />

Jan Howards Mike Edwards John Perkins<br />

0 1 2<br />

3 4 5<br />

Tiffany Allens Zack Lilly Bobby Canter<br />

ReDim Preserve BigArray(2)<br />

ReDim BigArray(2)<br />

Resizing an array erases everything currently stored in that array.<br />

If you want to resize an array and save the data in the array, you can use the<br />

Preserve command like this:<br />

ReDim Preserve BigArray(2)<br />

Not every <strong>programming</strong> language lets you resize an array and preserve its<br />

contents.<br />

C++<br />

To create a resizable array in C++, you have to go through slightly different<br />

steps.<br />

First, you must define a resizable array like this:<br />

datatype *arrayname;<br />

So if you wanted to create a resizable array of integers, you’d declare your<br />

array as follows:<br />

int *numberarray;<br />

Be<strong>for</strong>e you could store any data in this array, you’d have to define its size<br />

using the new command. So if you wanted to resize the array to hold six elements<br />

(numbered 0 to 5), you could use the following:<br />

int *numberarray;<br />

numberarray = new int[5];

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!