Getting a particular value from a vector
I have the vector private Vector players =new Vector();
The vector is of an unkown size and contains many objects of Score
class Scoreimplements Serializable
{
String playerName;
int playerScores;
public String toString ()
{
return playerName +" " + playerScores;
}
}
What I want to do is retrieve the 10th score object, particularly the integer as I want to compare it to another integer.
Any idea how I might be able to do this?
[874 byte] By [
Glosmana] at [2007-10-3 3:10:28]

I'm not entirely sure how I do that though.
I'm trying to print out the tenth score to show me that it works ok.
I have this so far but It doesn't compile and I'm not sure if i'm on the right track.....
int i = 10;
int tenthScore = (Scores)players.get(i);
System.out.println(tenthScore);
This Java_2006 guy is a complete idiot. Ignore both of his replies (the former addresses a different issue and the latter is redundant; all objects extend Object implicitly anyway as it's the root of the inheritance hierarchy).
> Thats the bit I dont know how to do. I still can't
> get the 10th object though.
Ok, you've filled a Vector with objects of type Score. So why are you trying to extract an object of type String from it? You should be extracting an object of type Score from it.
Once you have the object you will need to figure out how to access the score field from it.
I'm not going to hand you the answer on a plate, because that way you won't learn anything. But I'll give you a pretty robust set of hints:
// Hint #1
Score score = new Score();
score.playerName = "Hello there!";
score.playerScores = 42;
int theScore = score.playerScores;
System.out.println(theScore);
// Hint #2
String hint = "Hello!"; // Declare a String reference pointed to a String object
Object hintObj = hint; // Upcast a String reference to an Object reference
String hintString = (String)hintObj; // Downcast the object reference back down to a String reference
System.out.println(hintString); // Print the String.
// Hint #3
int i = 9;
Object obj = players.get(i); // Obtain an object reference to a Score object
// Downcast the object reference to a Score object
// Extract the playerScores field
// Print the playerScores field.