• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how I'm doing :
Vector xValues = new Vector();
double[] xArray = new double[10];
xValues.addElement(new Double(0));
xValues.addElement(new Double(1));
xValues.addElement(new Double(2));
xValues.addElement(new Double(3));
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

xArray = (double[])xValues.elementAt(i);
that is your problem. Use:
xArray = (Double)xValues.elementAt(i);
elementAt() returns an Object reference NOT a primitive.
Actually there is an API which I think would help you.
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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()
method instead. I haven't read the DOC on that but,
expect it to be pretty stratght forward.
Gregg:
Access to Vector is synchronized. This is very important
difference. On the other hand, access to a List
depends on the implementation. I am not sure what List
you are using.
regds.
- satya
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
found this article on javaworld..interesting read
http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar Bilgi:
found this article on javaworld..

In a comparison of Vector and ArrayList with respect to synchronization, it is quite perverse to omit any mention of Collections.synchronizedList(). Also no mention of the fact that the Vector API is a bloated combination of the JDK 1.1 API and the Collections API, which can be confusing for some. If you ask me a Vector is for legacy code only.
- Peter

[This message has been edited by Peter den Haan (edited September 28, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic