# Newbie question; Vectors

Printable View

• 08-02-2008, 07:21 PM
Kern
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;

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'.
• 08-02-2008, 08: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, 09: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;

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.
• 08-02-2008, 10:39 PM
Norm
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
Quote:

..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.
• 08-02-2008, 10:51 PM
Kern
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.
• 08-02-2008, 11: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:
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.
• 08-03-2008, 01: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, 07:59 AM
fishtoprecords
Minor nit, recent Java versions discourage Vector and replace it with ArrayList
It works the same, but has much more power