This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Vector Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector Question" Watch "Vector Question" New topic
Author

Vector Question

Buburub
Greenhorn

Joined: Apr 08, 2001
Posts: 8
When you subclass a vector can you put primitives in it?
(I know vectors only hold objects)
public class Subvector extends Vector
{
int i= 5; //does error occur here?
public static void main(String args[ ]){
Subvector sv = new Subvector( ) //does error occur here?
System.out.println(sv.i) //does error occur here?
}
}
Thanks in Advance!
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
Buburub,
You might want to adjust your username to comply with the
naming policy here. (They mention it when you sign up)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
All you have done is create a Vector that can hold one and only one int. Not a whole array of ints.
Clearly, based on the questions you have asked you need to go to the bunkhouse and buy a simple book on Java and start at the beginning. You seem completely lost.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Vectors can only hold objects. If you need to put a primitive in a Vector you need to wrap it in a Wrapper class.
Integer j = 5; //puts the int value in an Object wrapper.
Subvector v = new Subvector();
v.add(j);
In your example your i value is not "in" the Vector in the sense that the other elements are in it. When you load a Vector you load the elements to the "location" that the methods look to for working the Vector. For instance you can not use the sv.get(0) method of Vector to get your int value because it is outside of the location defined in the parent class. You can HAVE the int variable - it is just extra stuff.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
All of your "does an error occur here" questions can be answered by javac. Why not simply run the compiler rather than go to all the trouble to post to a newsgroup?


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector Question
 
Similar Threads
Vector conversion problem
Vector.toArray(object [] a) ?
Overriding return types
Vector Question!!
use of type in <jsp:usebean>