Accessing a method inside of an object that is stored in a vector...  
Author Message
da Vinci





PostPosted: 2004-12-5 9:23:00 Top

java-programmer, Accessing a method inside of an object that is stored in a vector... I have been trying to post to .help all day but nothing is going
through. Will try it here and hope it does....

I am trying to access a getData() method that simply returns a stored
integer value from an object that is stored in a vector. I have tried
to do this but cannot get the notation correct. I tried google groups
and the only thing I got from there was to pull the object from the
vector into a temperary object. That didnt work.

So, here is the code I have so far of making the temp object and
making sure it is actually pulling one out. I still connot figure out
how to access the getData() method.

Any hints are appreciated.

import java.util.Vector;

public class StackUser
{

public static void main(String argv[])
{

Vector vector = new Vector();

for (int i = 0; i < 100; i++)
vector.add(i, new StackNode());

System.out.println(vector.get(0));

Object tobj = vector.get(0);

System.out.println(tobj);


}
}

public class StackNode
{

private int value = 0;


public StackNode()
{
value = (int)(Math.random());
}

public int getData()
{
return value;
}

}
 
Raymond DeCampo





PostPosted: 2004-12-5 11:39:00 Top

java-programmer >> Accessing a method inside of an object that is stored in a vector... da Vinci wrote:
> I have been trying to post to .help all day but nothing is going
> through. Will try it here and hope it does....
>
> I am trying to access a getData() method that simply returns a stored
> integer value from an object that is stored in a vector. I have tried
> to do this but cannot get the notation correct. I tried google groups
> and the only thing I got from there was to pull the object from the
> vector into a temperary object. That didnt work.
>
> So, here is the code I have so far of making the temp object and
> making sure it is actually pulling one out. I still connot figure out
> how to access the getData() method.
>

After getting the object from the Vector, you must cast it to the proper
type. Look up the cast syntax in your reference material.

If you use the so-called "generics" in Java 1.5, the cast is unnecessary.

HTH,
Ray

--
XML is the programmer's duct tape.