Results 1 to 8 of 8
Thread: Newbie question; Vectors
- 08-02-2008, 06:21 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
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;
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'.Java Code:double sum = 0; for(int i=0; i<myVector.size(); i++){ sum = sum + myVector.elementAt(i); }
- 08-02-2008, 07:57 PM #2
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 #3
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
How can I do that ??You need to cast the object that comes out of the Vector to the type that you put into it.
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;
...doesn't work. What I don't understand is that I can't get an integer from myVector.elementAt, or a double, or anything.Java Code:int sum = 0; for(int i=0; i<myVector.size(); i++){ sum = sum + (int)(myVector.elementAt(i)); }
- 08-02-2008, 09:39 PM #4
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 messagecopy and paste it here. There are a LOT of ways for a program to NOT work...doesn't work
Vectors can only store Objects. int is not an Object. Find a class to hold your numbers.
- 08-02-2008, 09:51 PM #5
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
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, 10:59 PM #6
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.Java Code:// preliminary Vector<Double> DoubleBubba = new Vector<Double>(); for(.........) { Double nextDouble = ( Double ) DoubleBubba.getElementAt(index);
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
- 08-03-2008, 12:00 AM #7
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 #8
Similar Threads
-
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM -
Newbie question about Static methods
By SCS17 in forum New To JavaReplies: 9Last Post: 02-06-2008, 08:03 AM -
Help with Vectors and Strings...
By kaban in forum New To JavaReplies: 2Last Post: 12-09-2007, 09:04 AM -
Vectors vs ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 11-28-2007, 10:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks