• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vector Question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic