File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes some queries in the program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "some queries in the program" Watch "some queries in the program" New topic
Author

some queries in the program

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
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
Rob Harris
Greenhorn

Joined: Nov 11, 2004
Posts: 10
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
 
Consider Paul's rocket mass heater.
 
subject: some queries in the program
 
Similar Threads
ArrayList
how to remove '\n' and '\r' in the file by kshell
Array problem
The hashCode() method of java.lang.Object
Generics Question - Reference type