Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Adding integer values to vector elements

    Hey guys,

    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.

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •