-
on vectors
first of all, thanks to all who've helped me thus far... i really appreciate it.
1. how do i determine the number of elements in a vector?
i tried:
i = vectorName.elementCount();
but it doesnt work.
2. if the vector stores user-defined objects (and these objects consist of int, string, date etc), can i access a particular attribute of an object in the vector directly? i.e.
int retrieved;
retrieved = vectorName.elementAt(0).age; //where age is of int type
-
1.
i = vectorName.size();
2.
Sure you can
retrieved = ((ClassOfStoredObject)vectorName.elementAt(0)).age;
should be working (haven't tried it :))
-
Code:
import java.util.Vector;
class X{
private String greeting;
public X(String greeting){
this.greeting = greeting;
}
public String getGreeting(){
return greeting;
}
}
public class Z {
public static void main(String[] args){
Z z = new Z();
z.invoke();
}
private X x;
private Vector v = new Vector();
private void invoke(){
v.addElement(new X("Hello"));
x = (X) v.elementAt(0);
System.out.println(x.getGreeting());
}
}