• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Vectors(again)

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(A blast from the past.) Thanks, a nice simple answer.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic