Alrighty then, I said I would explain the difference between size and capacity, so here it is:
"capacity is how much room for elements there is in a Vector, while size is how many actual elements there are"
It's kind of like an egg carton: you can have space for 6 or 12 or 18 eggs, but its possible that NONE of those spaces have been filled. Right now, we have an empty egg carton. SO, the next step is to FILL the carton. Here's how:
A) the first step is to dcelare a button we can click and add an element to the Vector:
Button newOne;//a button for making new vector elements
B) we will also need another TextField, so we can create some stuff that will go in the Vector:
TextField newElement;
C) Next, we will need to create both this button and the TextField, and add them to the Frame. This oughta be easy for you by now, so I won't spell it out. The only thing I ask is that your TextField's initial text should read something like this:
"type here to add to Vector"
D) Last, add yet another "else if" to your actionPerformed, like so:
else if (e.getSource() == newOne)
{
vNess.addElement(new String(newElement.getText() ) );
//take what is typed in TextField and make it a Vector element
}//add element to Vector
What this does is to take whatever you type into the new TextField, and add it as an element in your Vector!!
OK, re-compile, and run the program. Type some stuff into the new TextField, and use the "newOne" button to add that String to your Vector.
After you've played with that, use the "howMany" button to see if the size of your Vector has increased
Cheers,
Uncle Paulie
Thursday, February 19, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment