Int to String ...

Status
Not open for further replies.

R2P2

Ars Praefectus
3,783
Subscriptor
Originally posted by Guito:
Doesn't a quick Integer.parseInt() take care of that?
No, that's for parsing Strings into ints. I wanted to put ints into a Vector, and get them back out as ints. With Java, I'd have to wrap the ints in Integers, put the Integers into the Vector, then unwrap the Integers into ints when I took them out of the Vector. Or convert the ints to Strings, and then use Integer.parseInt() to parse them back in to ints after taking them out of the Vector. Like I said, Pain In The Ass.
 

R2P2

Ars Praefectus
3,783
Subscriptor
Originally posted by wet behind the ears:
Use an array[] instead of a Vector. Vectors aren't dynamic unfortunately. I think you're stuck with converting int primitive into an Object though.

wbte
I think you meant arrays aren't dynamic, and that's exactly why I wanted to stick ints into a Vector -- I wanted to build a list of ints, having no idea of how big the list would get, so dynamic-ness was important.
 

R2P2

Ars Praefectus
3,783
Subscriptor
Originally posted by AirWhale:
Kinda off topic, but close enough to put it here.
I was taught way back in my wery first programing class that the Java vector class was inefficent and that it was faster to build your own dynamic arrays. Was I taught wrong?
For someone taking their first computer course, building your own dynamic array would be a lot slower to code than using Vector, and your implementation would probably run slower than Vector, too. Teachers just like to make you work. ;)
 

bigdruid

Ars Scholae Palatinae
731
Y'all be using ArrayLists anyway instead of Vector. Vector is *so* 1999 :)

Seriously, though, I'd imagine that accessing an element of a Vector/ArrayList might be *slightly* slower than directly accessing an array (you have the overhead of a get() member function call or an Iterator.next() call) instead of a direct array access, but I wouldn't really call it "inefficient".

-druid
 

Midlands

Ars Praetorian
535
Subscriptor
Originally posted by AirWhale:
Kinda off topic, but close enough to put it here.

I was taught way back in my wery first programing class that the Java vector class was inefficent and that it was faster to build your own dynamic arrays. Was I taught wrong?

Vector (i.e., java.util.Vector) is synchronized, which is why it is slow. Especially in old JDK versions, synchronization introduced a lot of overhead. Nowadays, it's much better.

I'd still only advocate writing your own if you'd already tried Vector and discovered that it actually was too slow, rather than just thinking it would be too slow.

These days, we have ArrayList, which is good.
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Is there really much of a difference in speed between Vector and ArrayList? The program I'm working on uses a helluvalotta Vectors, and faster is always nice. Someone put a couple of ArrayLists in there, but I had no idea why anyone would use one, until now.

ArrayLists will probably be a little faster in most cases. However, you have to consider that, as stated previously, Vectors are synchronized, while ArrayLists are not. If you are doing any access to these structures in a multithreaded environment, you're much better off sticking with Vectors.
 
Status
Not open for further replies.