Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector Question

 
Buburub
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic