• 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

some queries in the program

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
consider the following program:
import java.util.*;
public class VecNum
{
public static void main(String argv[])
{
Vector v = new Vector();
v.add(new Integer(1));
v.add(new Integer(2));
for(int i=0; i < v.size();i ++)//line1
{
Integer iw =(Integer) v.get(i);//line2
System.out.println(iw.intValue());//line3
}

}

}

In the above program i have some queries:
1.why we need to use vector for wrapping all the integer values
2.what is the purpose of using line1,line2,line3
3.when i compile this program its generating errors

tx
carl..
tx
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carl,

1. You're not using a Vector to wrap your integer values, you're using the Integer Wrapper class. This turns your int primitives into Objects so they can be added to the collection.

2. Lines 1, 2 and 3 retrieve the Integer objects from the Vector. Vector.size() returns the number of components in the Vector and get() retrieves the objects using their index (just like an array).

3. The code compiles and runs fine for me.

Hope this helps!

Rob
SCJP 1.4
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic