This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Vector reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector reference?" Watch "Vector reference?" New topic
Author

Vector reference?

Stefan Müllerheim
Greenhorn

Joined: Sep 17, 2010
Posts: 12
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:


less is more.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1903
    
    7

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().

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jeganathan Palanisamy
Greenhorn

Joined: Sep 17, 2010
Posts: 3
Jst explored the implementation of toString() in Vector class its implementation is:


So don't expect the object name from vector toString() method


http://jeggu.com
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19968
    
  25

Don't forget Integer.toHexString to change the hash code into a hexadecimal string.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1903
    
    7

Aaaaaaw, now it's not a challange anymore!
Just a matter of concatentating Strings...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41868
    
  31
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

Joined: Oct 27, 2005
Posts: 19968
    
  25

Although that's true that just shifts the original problem; ArrayList has a similar String form as Vector.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41868
    
  31
Can you get the ClassName@hash type String by casting a reference back to java.lang.Object?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1903
    
    7

Thankfully not.
Stefan Müllerheim
Greenhorn

Joined: Sep 17, 2010
Posts: 12
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

Joined: Oct 27, 2005
Posts: 19968
    
  25

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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector reference?