programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Converting an array to a Vector -> Simple question with many doubts

Ranch Hand
Posts: 122
• Number of slices to send:
Optional 'thank-you' note:
Hi
I have an array like this :
double[]{2, 4, 6, 8}
How do I convert this array to a Vector & retrieve the elements back from the Vector into an array back again ?
Thanks
Meghna

Ranch Hand
Posts: 2823
• Number of slices to send:
Optional 'thank-you' note:
You have to use wrappers for the double array. Vector does not accept primitives. Is there a reason you can't use ArrayList? Either way you have to convert each element of double[] to Double.

Meghna ks
Ranch Hand
Posts: 122
• Number of slices to send:
Optional 'thank-you' note:
This is how I'm doing :
Vector xValues = new Vector();
double[] xArray = new double[10];
for(int i = 0; i != xValues.size(); i++)
{
xArray = (double[])xValues.elementAt(i);
}
But I'm getting a ClassCastException inside the for loop. All I need to do is store the elements of type double in the Vector since I don't know the size initially and copy it to an array of type double and pass it to one of my functions as an array. Any suggestions ? Please advise
Thanks
Meghna

Ranch Hand
Posts: 5040
• Number of slices to send:
Optional 'thank-you' note:

xArray = (double[])xValues.elementAt(i);
xArray = (Double)xValues.elementAt(i);
elementAt() returns an Object reference NOT a primitive.
You could also look into the toArray() methods on the
Vector class.....
Sorry, I if I am being....right now
I can't find help for myself and am upset on seeing
such a qstn...no offense but a little homework would have helped...
- satya

Ranch Hand
Posts: 15304
6
• Number of slices to send:
Optional 'thank-you' note:
I currently have been using Lists instead of Vectors. Are there any reasons I should be using Vectors instead? The API looks very similar on both.
btw - I am using Lists instead of Arrays also, because of it being dynamic.
Thanks

------------------
Happy Coding,
Gregg Bolinger

Meghna ks
Ranch Hand
Posts: 122
• Number of slices to send:
Optional 'thank-you' note:
Hi
No offense, I did do my Homework on this, but since I could'nt figure out I was expecting some help. I did realise that I had committed a syntax problem in the soln. u've suggested, but the problem here is that my array is of the type primitive double. If I return a an object of type Double, I would'nt be able to store it back in the array.
I needed some help wrt the following statement :
xArray = (Double[])xValues.elementAt(i);
Here since the array declaration is double[], there would definitely be a casting problem. This is what I needed to analyse.
FYI : You don't have to be upset with my qn. I guess I do have my choice to put my question on JavaRanch and I have great respect for this site. I think I would'nt have posted this question without doing my Homework.So, please try to be a little more modest in your replies. It would definitely be a little encouraging. I'm sorry if I've misread your reply otherwise.
Thanks
Meghna

Meghna ks
Ranch Hand
Posts: 122
• Number of slices to send:
Optional 'thank-you' note:
Hi Gregg
Thanks for the prompt response, but my requirement demands I use
Vectors. So, I'm sticking with it. But if you happen to come across any help, please do let me know.
Thanks
Meghna

Paul Stevens
Ranch Hand
Posts: 2823
• Number of slices to send:
Optional 'thank-you' note:
You can create the primitive array by using the size of the Vector. To put the values into the array from the Vector
double[] test = new double[vector.size()];
//loop here

test[i] = ((Double)vector.elementAt(i)).doubleValue();
//end loop

Gregg ArrayList and Vector are very similar. Vector is synchronized. So if that is not required ArrayList is a better option (performance wise).

[This message has been edited by Paul Stevens (edited September 27, 2001).]
[This message has been edited by Paul Stevens (edited September 27, 2001).]

Meghna ks
Ranch Hand
Posts: 122
• Number of slices to send:
Optional 'thank-you' note:
Yeah Paul
I just figured it out and found the solution. I did the following which is similar to what you've suggested :
for(int i = 0; i != xValues.size(); i++)
{
Double vectorValues = (Double)xValues.elementAt(i);
xArray[i] = vectorValues.doubleValue();
}
Thanks a lot for the suggestion.I appreciate all of your help.
Thanks
Meghna

Ranch Hand
Posts: 5040
• Number of slices to send:
Optional 'thank-you' note:

Meghna:
I was upset that you were asking such
a fundamental qstn. Anyways, its my personal
opinion and you could care less. Din't mean to
attach personally...Sorry, if you felt that way!
Having said that...

but the problem here is that my array is of the type
primitive double. If I return a an object of type Double, I would'nt be able to store it back in the array.

You shouldn't do this. IMHO you should use the toArray()
expect it to be pretty stratght forward.
Gregg:
depends on the implementation. I am not sure what List
you are using.
regds.
- satya

Paul Stevens
Ranch Hand
Posts: 2823
• Number of slices to send:
Optional 'thank-you' note:
toArray() returns an Object[]. He could cast it to a Double[] but I don't think he could cast it to a double[]. Why does it have to be a primitive array?

Gregg Bolinger
Ranch Hand
Posts: 15304
6
• Number of slices to send:
Optional 'thank-you' note:
I use java.awt.List for String Object storage where an object needs to be added or removed for one reason or another.
For example, if I were to write a Poker Application, I would probably use a java.awt.List to store the players Hand.

------------------
Happy Coding,
Gregg Bolinger

Ranch Hand
Posts: 5040
• Number of slices to send:
Optional 'thank-you' note:

I guess I do have my choice to put my question on JavaRanch

on second thoughts, I am moving this thread to the Java in General (Intermediate forum).
Please continue the discussion in that forum.
Sorry for any inconvenience.
Thanks.
- satya

Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html?

author
Posts: 3252
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Sagar Bilgi: