PDA

Click to See Complete Forum and Search --> : on vectors


quipy
Apr 30th, 2003, 11:28 AM
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

CreoN
Apr 30th, 2003, 04:19 PM
1.
i = vectorName.size();

2.
Sure you can
retrieved = ((ClassOfStoredObject)vectorName.elementAt(0)).age;
should be working (haven't tried it :))

Dillinger4
May 1st, 2003, 12:21 AM
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());
}
}