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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

storing ints in vectors

Status
Not open for further replies.

ewan91

Programmer
Jun 25, 2001
13
GB
hi

i am storing ints in a vector by wrapping in class Integer

swingsStore.addElement( new Integer (swing) );

but i am having trouble extacting the value, i don't want to change it just store then extract all the values
here it what i currently have

do
{
int size = swingsStore.size( );
totalSwing = Integer.parseInt( swingsStore.elementAt( size - 1)); // this is the line causing problems
swingsStore.removeElementAt( size -1 );


} while ( swingsStore.size( ) != -1 );


but it does not extract the values
any ideas

thanks
ewan
 
Use intValue(). Integer.parseInt(String s) accepts and String and returns an Integer.

Code:
int totalSwing;
do 
{
    int size = swingsStore.size( );
    totalSwing = ((Integer)swingsStore.elementAt(size - 1)).intValue();    
    swingsStore.removeElementAt( size -1 );
} while ( swingsStore.size( ) != -1 );

Also you should be using an iterator to loop through your collections. I will leave this as an exercise for you.
Wushutwist
 
removeElementAt(int) will actually destroy the element, is that what you want?

If you actually need it to return the Integer object you have placed in the vector, you will need elementAt(int).

If you want the value of the Integer object, you will first need to get hold of the object using elementAt(int) and then use the Integers' intValue() method to actually retrieve the value in the form of an int.

I don't know if there is a quicker way.

 
why don't you use an int array for storage? using a vector and all those immutable Integer/Sting classes is very heavy on memory and, from what i can tell, you don't need them all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top