This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Vectors(again) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vectors(again)" Watch "Vectors(again)" New topic
Author

Vectors(again)

Bill Norton
Greenhorn

Joined: Mar 29, 2001
Posts: 27
Once again I come to JavaRanch for help. :-)
Ok, I have created a vector in class.
But I can't seem to get any of the methods in my vector to work.
Vector v = new Vector();
int z=0;
z = v.elementAt(1).weirdMethod();
Assume weirdMethod works fine and returns an int.
I get a compiler error: unresolved symbol at, z= v.elementAt(1).weirdMethod();
It seems from this that you would get compiler errors all the time, since the Vector wouldn't know what kind of objects are put in, therefore couldn;t find the method.
::Sigh::
I think I am missing something pretty basic here, I seem to always leave my questions short, so don't be afriad to ask for more information.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
To do this kind of things you need to cast your object you can cast the Object from the specific Class if you know it or to an interface implemented by all the Object in your Vector;
So for me test this

Vector v = new Vector();
int z=0;
AnObjectWithWeirdMethod myOject = new AnObjectWithWeirdMethod();
v.add(myObject);
z = ((AnObjectWithWeirdMethod)v.elementAt(0)).weirdMethod();

hope it helps

------------------
Benjamin l´┐Żonard
evisor
Bill Norton
Greenhorn

Joined: Mar 29, 2001
Posts: 27
That's it!
I thought about casting it before, but didn't realize that you could cast part of the dot notation.
Answer seems obvious now.
Thanks!
Bill Norton
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Why is it that in Java you can't have a vector of a certain type - so that you wouldn't have to cast everything on the way out. Is this so that things can be resolved at compile time?
Using STL in C++ you can have a vector of class A, which is nice.
Thanks.


Please ignore post, I have no idea what I am talking about.
Niklas Junel
Greenhorn

Joined: Dec 19, 2001
Posts: 11
If you want a Vector for a specific class, you can write your own wrapperclass. Here is a class that take the primitive type int and put it in a Vector, there also is a method to get the values back as an int.

------------------

[This message has been edited by Cindy Glass (edited December 19, 2001).]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

(A blast from the past.) Thanks, a nice simple answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vectors(again)
 
Similar Threads
Comparator
returning mutiple values
logical error
Can I read from a Vector and writer into a File..
Difference in JSP