Results 1 to 3 of 3
Thread: vector - get
- 01-02-2009, 02:46 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
vector - get
Hi,
I'm trying to return the name of an object that I have added to a vector. I'm using
But this returns:Java Code:Vector<animal> cats = new Vector<animal>(); cats.addElement(new cat(0)); System.out.println("Type of object " + cats.get(i));
Type of object arrayofobjects$cat@10b30a7
And I need it to return something more like "Type of object: Cat"
how can I do this?
Thanks,
Nate
- 01-02-2009, 03:01 AM #2
Java Code:import java.util.Vector; public class Test { public static void main(String[] args) { Vector<Animal> v = new Vector<Animal>(); for(int i = 0; i < 3; i++) { v.add(new Cat(i)); } for(int i = 0; i < v.size(); i++) { System.out.println(v.get(i)); } } } class Animal { public String toString() { return getClass().getName(); } } class Cat extends Animal { int n; public Cat(int n) { this.n = n; } public String toString() { return super.toString() + "[n: " + n + "]"; } }Last edited by hardwired; 01-02-2009 at 07:13 AM. Reason: Added missing quote.
- 01-02-2009, 04:09 AM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
seems missing quot in third last line
should be return super.toString() + "[n: " + n + "]"; ?
Similar Threads
-
Vector create
By Warren in forum New To JavaReplies: 4Last Post: 03-02-2010, 02:42 AM -
Vector problem
By Ace_Of_John in forum New To JavaReplies: 1Last Post: 01-27-2008, 08:53 PM -
Vector help
By king_arthur in forum New To JavaReplies: 3Last Post: 01-22-2008, 07:33 PM -
Declaring Vector
By mew in forum New To JavaReplies: 1Last Post: 12-05-2007, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks