• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

problem in vector

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there
Actually I have a vector in which I have stored some id(which r integer) now I want to assign those int to a variable called bookid but when I try to do that it give me error :
" Incompatible type for declaration. Can't convert java.lang.Object to int.
int bookid = add.elementAt(i); "

my code is
Vector add = new Vector();
add.addElement(4);
add.addElement(3);

int size=add.size();

for(int i=0;i<size;i++)>
{
int bookid = add.elementAt(i);
system.out.println()bookid;
}
I know this is very basic pls tell me how to solve this problem
Thanks in advance
Preeti
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peetri,
Try using the class 'Integer' rather than the primitive type 'int'. 'addElement' takes an Object not a primitive type.
For example,
Vector add = new Vector();
add.addElement(new Integer(4));
add.addElement(new Integer(3));
int size=add.size();
for(int i=0;i {
Integer bookid = (Integer)add.elementAt(i);
system.out.println(bookid);
}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai preeti,
I dont think u will go to that statement there should b errors b4 this statement itself ok.vector accepts Objects not the primitive type arguments.try out the solution given above works perfectly
 
Preeti Sikri
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys I got the solution
 
I AM MIGHTY! Especially when I hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic