• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int[] find : converting Vector to int[]

 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a vector containing "Integer(wrappers)".

I want to convert this vector to an array of primitive int's.

I don't want to call Integer.getValue for each separate element.

toArray of Vector mandates the type passed to be an Object array. It is not accepting an int array.

Can somebody please tell is there a way around?

Thanks in advance.
Jiju
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I'd stop usig Vector and HashTable, then are not deprecated, but the old way of doing things. Use ArrayList and HashMap instead.

Second, all the "Objects" in the Vector are Objects, not primitives. So no matter what you would need to convert the Wrapper objects to their primitives, or keep the array as an array of Wrapper objects, and only convert to primitive when you need it.

Secondly, in Java 5.0 Tiger, they have a new feature called auto-boxing and un-boxing, which converts between Wrapper objects and the primitives without having to code that.

Third, no third needed, I figured out my answer.

Mark
[ October 28, 2004: Message edited by: Mark Spritzler ]
 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem your facing. I didnt know about the new boxing unboxing feature so I just resorted to using an array straight out. Just use two array and a for loop .
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Inuka and Mark. I will check with java 5 tiger.
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Mark on not using Vector.

I don't know the real reason behind it. But many recommends the same. If you know what is it about please let me know.

[LINK]
http://www.javapractices.com/Topic65.cjp
[/LINK]
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found a similar discussion at
[link]
http://www.coderanch.com/t/201801/Performance/java/Hashtable-Vector-problems
[/link]
 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector and Hashtable are syncronized there for they are slower in performance where as HashMap and ArrayList or straight array is not and they are fater in performance.

on the side Hashtable has a habit of lingering around.......
[ October 29, 2004: Message edited by: Udayan Patel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic