Topics: Vector Class in Java
Topics: Vector Class in Java
F:\>javac VectorDemo.java
Note: VectorDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
<<OUTPUT>>
F:\>java VectorDemo
0
5
6
10
7 Object-Oriented Programming Using Java
Some Facts About Un-
parameterized Vectors
• Elements of any type are added and retrieved only in ‘Object’ type.
• You have to type cast the element to its base type before use.
• Example : Sum of numbers stored in vector
import java.util.*;
class VectorDemo F:\>javac -Xlint VectorDemo.java
{ VectorDemo.java:8: warning: [unchecked] unchecked call to
add(E) as a member of
public static void main(String args[]) the raw type java.util.Vector
v1.add(10); v1.add(20); v1.add(30);
{ ^
Vector v1 = new Vector(5); VectorDemo.java:8: warning: [unchecked] unchecked call to
add(E) as a member of
the raw type java.util.Vector
v1.add(10); v1.add(20); v1.add(30); v1.add(10); v1.add(20); v1.add(30);
^
VectorDemo.java:8: warning: [unchecked] unchecked call to
add(E) as a member of
double sum = 0; the raw type java.util.Vector
for(int i =0; i < v1.size(); i++) v1.add(10); v1.add(20); v1.add(30);
^
sum = sum + v1.get(i); VectorDemo.java:12: operator + cannot be applied to
double,java.lang.Object
sum = sum + v1.get(i);
}// End of Method ^
1 error
}// End of VectorDemo class 3 warnings