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;

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'.

08-02-2008, 07:57 PM

Norm

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?

08-02-2008, 08:46 PM

Kern

Quote:

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;

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.

08-02-2008, 10:59 PM

Nicholas Jordan

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:

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.

08-03-2008, 12:00 AM

Norm

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.

08-03-2008, 06:59 AM

fishtoprecords

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