Results 1 to 8 of 8
  1. #1
    Kern is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default Newbie question; Vectors

    Hello, I'm a former MatLab user and I'm struggling with very basic operations in JAVA, right now I need a dynamic array (variable size), so I used java.util.Vector. But now I need to get the sum of all elements in the vector, how on earth can I do that ?

    As I don't see anything to sum all the element in the vector class, I tried;

    Java Code:
    double sum = 0;
    for(int i=0; i<myVector.size(); i++){
        sum = sum + myVector.elementAt(i);
    }
    But it doesn't work because elementAt return an object. I don't really know how it works, because this vector is, in reality, only made of 'double'.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    You get back from a Vector what you put into it. You don't show what you've put into it so I don't know how to handle what comes out of it.
    You need to cast the object that comes out of the Vector to the type that you put into it.
    double is a primitive and can't be treated as an Object to be put into a Vector. Do you mean Double?

  3. #3
    Kern is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default

    You need to cast the object that comes out of the Vector to the type that you put into it.
    How can I do that ??

    myVector is made of element added with myVector.addElement(t), with t;

    int t = 0;

    So it's an integer (but I tried making it a double, and it changes nothing). I really have no idea how to do this;

    Java Code:
            int sum = 0;
            for(int i=0; i<myVector.size(); i++){
                sum = sum + (int)(myVector.elementAt(i));
            }
    ...doesn't work. What I don't understand is that I can't get an integer from myVector.elementAt, or a double, or anything.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    What version of java are you using? The newer version has a feature (boxing) that will save you keystrokes but will confuse you if you don't know about it.
    When you get an error message
    ..doesn't work
    copy and paste it here. There are a LOT of ways for a program to NOT work.

    Vectors can only store Objects. int is not an Object. Find a class to hold your numbers.

  5. #5
    Kern is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default

    I use the lastest version;

    Java: 1.6.0_04; Java HotSpot(TM) Client VM 10.0-b19
    With NetBeans IDE 6.0 (Build 200711261600)

    It says incompatible types. I would use a normal array, but I can't add any new elements. The whole point of using vectors is that I can do;

    Vector myVector = new Vector(0);

    And add new elements with addElement, as far as I know, you can't do that with an array.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post

    basically you are very much on the right track, there are some computer science issues that things like matlab and pascal and lisp and basic and several other languages take care of for you.

    To get a double in and out of an array as a Vector so that the array is re-sizeable, you probably have to do the cast:

    Double doubleBubba = ( Double ) VictorTheVector.getElementAt(index);

    Here, the index is what we call a primitive type which takes some idea of how the hardware works to explain that vis-a-vis the Double. Thus, according to your problem description, the code would go thus:
    Java Code:
    // preliminary
    Vector<Double> DoubleBubba = new Vector<Double>();
    for(.........)
    {   
        Double nextDouble = ( Double ) DoubleBubba.getElementAt(index);
    In this overly abbreviated snippet, the index is an int, which is the most common data type ( at least for this discussion ) and indexing of the Vector is done with an int same as a simple array. The double data-type is different from the Double data-type. They will confuse you with their explainations of the difference.

    Suffice it to say the Double datatype has an address, the int just gets thrown around with no wrapper. Code is just to show concepts and should be written by looking into the documentation to find the correct method calls.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Unless you post the full text of the error message and the code that the error messge refers to, it is very hard to guess what you are doing and how to change it.
    My crystal ball is in the shop this month and I don't know if I'll ever get it working properly again.

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Minor nit, recent Java versions discourage Vector and replace it with ArrayList
    It works the same, but has much more power

Similar Threads

  1. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 02:57 PM
  2. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 08:03 AM
  3. Help with Vectors and Strings...
    By kaban in forum New To Java
    Replies: 2
    Last Post: 12-09-2007, 09:04 AM
  4. Vectors vs ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-28-2007, 10:29 AM

Posting Permissions

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