Forums Register Login

vector methods and character input

+Pie Number of slices to send: Send
1. I have a class named 'node' and a vector made up of nodes. When I use a vector method like lastElement(), an Object is returned, not a node. However, I need a node to manipulate with as a result of this method call - for eg, I assign the node that I would like returned from the method to a newly created node. How do I tackle this?

2. How do I read character input from the console? i.e., objects of type 'char'?

Thanks.
+Pie Number of slices to send: Send
In response to number 1, cast it as a Node:
if nodeVector is your vector,

(Node)nodeVector.getLastElement() will return a Node instead of an object

All java Collections work like this. (they pretend not to know what kind of objects they are holding and they make you tell them)
+Pie Number of slices to send: Send
Cast the Object type returned from lastElement() to a Node type.

A char is a standard type, not an Object or reference type. You can read them using an implementation of java.io.Reader.read() to read System.in. Like this:

+Pie Number of slices to send: Send
If you are positive that only Nodes will be in the vector, Tim's solution is perfect. if there is ANY chance there might be something OTHER than a node in there, you may want to use the 'instanceOf' operator to make sure before you do your cast.
+Pie Number of slices to send: Send
Thanks much.
+Pie Number of slices to send: Send
If you're lucky enough to be using J2SE 5 (Tiger, aka JDK 1.5), you can use generics:

Vector<Node> myVector = new Vector<Node>();
myVector.add(new Node());
...
Node aNode = myVector.lastElement();

Note that here the cast is unnecessary!
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1225 times.
Similar Threads
Using a Vector within a Vector
how to fix the weird characters from displaying in xsl output?
Results are being retained in memory!
Problem!
Draw binary tree structure
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:46:58.