Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector reference?

 
Stefan Müllerheim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i' m using a lot of Vectors for storing other references. but if i print the var of the vector, the entire content is printed out with a long listing of every object reference.

is there a way to get a reference of the vector?

for example:


output:


instead i' m looking for such as:
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because java.util.Vector overrides the default toString() implementation provided by java.lang.Object.
If you want to reconstruct the output of the default implementation you should have a look at java.lang.Class#getName() and System.identityHashCode().
 
Jeganathan Palanisamy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jst explored the implementation of toString() in Vector class its implementation is:


So don't expect the object name from vector toString() method
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because Vector is overriding Object#toString(). If you look at this method's API, you'll see how to output what you want.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget Integer.toHexString to change the hash code into a hexadecimal string.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaw, now it's not a challange anymore!
Just a matter of concatentating Strings...
 
Campbell Ritchie
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Jeganathan Palanisamy and Stefan Muellerheim.

Why are you using Vector at all? It is a legacy class; you ought to use ArrayList in new code.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although that's true that just shifts the original problem; ArrayList has a similar String form as Vector.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you get the ClassName@hash type String by casting a reference back to java.lang.Object?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankfully not.
 
Stefan Müllerheim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow, so much answers! that makes me happy!

i have read every answers, but couldn't make it with toString (beginner at work).

but actually my concern is to get an unique identifier. with getClass().hashCode() i have got apparently what i need. it didn't change so far.

but i'm a bit afraid that the hashcode could change by adding of new objects or s.th. else?

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Muellerheim wrote:but actually my concern is to get an unique identifier. with getClass().hashCode() i have got apparently what i need.

You do know that getClass().hashCode() returns the hash code of the Class object, not the object itself? All you need from the class is its name.

but i'm a bit afraid that the hashcode could change by adding of new objects or s.th. else?

True, that's why Jelle suggested a different method for retrieving the hash code. And please UseRealWords: "something", not "s.th".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic