Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic arrays

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
0
0
BE
Hi,

I need to be able to have an array of which I don't know how big it has to be... It should be dynamic... Isn't there a way to just say that the array needs to expand when necesarry? I don't know how many cell will be needed !! I have a prog that reads input, and i need it put that info in an array, but the input can alot of info or it could be very short... What do I need to do? I need an array of which the size is variable, it needs to read input untill the user stops !!

PLEASE HELP !!
thanx,
math
 
it would probably be easier to use a Vector as the man says, they are designed to be this flexible.

You cannot dynamically change the size of an array by adding to it, you can only nullify elements of it i.e. make it smaller.

I had a similar problem once and got around it by basically nulling out the whole array (after copying all elements to a temp array) and then created the array again but this time 1 bigger - then copied the elements in the temp array into the new one. It worked really well, but with big sizes there will be a processing overhead that can be overcome by using a Vector.
 
Defenitly Vector, as it can be as big as you want without predefining the size..
basic commands to help out:
-To create a vector:
Vector myVect = new Vector();
-To add elements(! they must be objects, like a String.!!)
vect.add(object-to-add);
or
vect.addElement(object);
vect.add(int-index, object-to-add);
-To get an element at a certain position:
vect.elementAt(int-index);
vect.size() // return the number of elements in the vector
vect.clear(); // clear ALL elements in the vectors..
Little trick..to put and integer in a vector do this
vect.add(new Integer(int c=2));

voila..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top