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!

accessing vectors

Status
Not open for further replies.

ewan91

Programmer
Jun 25, 2001
13
GB
hi there

i'm storing ints in a vector and want to be able to work out the average of the int values
the functions below should do it but i am only getting the last value returned not the average of all of them

can anyone see where i'm going wrong


public void averageSwing ( )
{
int totalSwing = 0;
int temp = 0;
int numOfResults = swingsStore.size( );

for ( int i = 0; i < numOfResults; i++ )
{
Integer total = getVect( i ); // gets the value as an Integer
temp = total.intValue( ); // gets int value from Integer
totalSwing += temp; // works out total
}

average = totalSwing / numOfResults; // works out average

for ( int j = 0; j < numOfResults; j++)
{
swingsStore.removeElementAt( j );
}

}


public Integer getVect( int i )
{
return (Integer ) swingsStore.elementAt( i ); //gets the Integer value
}


public int getAverageSwing( )
{
return average; // returns the average value
}



thanks

ewan
 
it is difficult to say without seeing where and how you inialise your vector elements.

although I can see what i would consider to be superfluous coding, i can't see any reason why you would only get the last element, unless there was only one lelemnt in your vector.
 
Second Pipk's thoughts, there is bunch of unnecessary stuff mucking things up.

My suggestions, use an Iterator. This is what they are there for, check JavaDocs if you are not sure what they are. Two, put in some println statements are run this thru a debugger so that you can see what is happening in your For loop.
Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top