Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
10-28-2008, 01:51 PM #1
- Join Date
- Aug 2008
- Thanked 0 Times in 0 Posts
Adding integer values to vector elements
I am having a bit of trouble with a little java project that you can hopefully help me with.
The basic idea is to have a vector with 4 elements, each element is a number. I wish to update the vector with new numbers but I do not wish to change the element order.
For example I have the value 2 at element 0, I wish to add 2 to this, making the element at 0 now 4.
I have been searching around and cant seem to find out how to do this, any suggestions?
Thanks in advance, Robert.
10-28-2008, 09:09 PM #2
- Join Date
- May 2006
- Ontario, Canada
- Thanked 20 Times in 20 Posts
Here's a link to the Vector API which shows you all the methods available to a Vector. http://java.sun.com/j2se/1.3/docs/ap...il/Vector.html
You should be able to find methods here that'll help you solve your problem, but I'll give you some pseudo code as well.
1. Get the first element of the vector and store it in an int variable. Look at the methods firstElement or elementAt.
2. Take the value returned in step 1. and add to it.
3. Replace the first value of the Vector with the new value from step 2. Look at the methods set and setElementAt.
There's other ways to solve this problem as well, but this will hopefully give you a good starting point.