Reputation: 83
I read through the java doc fro Vector and cant see how I can return the index number of a vector after I have just added an element to it . Im trying to do something along these lines :
vec.addElement(new Person(Number,firstName,familyName));
int lastPersonIndex = vec.RETURN THE LAST INDEX();
I tried using lastElement but that returns an object and I want it to be stored in an int.I couldnt work out how I cast this (cant cast an object to an int can you?)
you can see im at beginner level , so any help appreciated
N.B just to add i know there are newer ways that are recommended than using vectors but this is part of a homework and we were taught vectors so I want to use them within a piece of work im doing
Upvotes: 0
Views: 85
Reputation: 496
You should return the vector size - 1:
int lastPersonIndex = vec.size() - 1;
Upvotes: 1
Reputation: 201447
Return the size of the Vector,
int lastPersonIndex = vec.size() - 1; // minus 1 because of 0 based counting.
Upvotes: 2