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]
# 1
get
dcmintera at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 2

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);

Glosmana at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 3
The first element is element zero, not one.
dcmintera at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 4
Glosman ,Is that u want the 10th score object in the vector list or u want the score object with the playersscore = 10 from the vector list.?
sound_ina at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 5
Also that doesn't compile because you're trying to use a Scores object as an integer, and because there's no such object as Scores anyway.
dcmintera at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 6
I want the integer of the 10th score object only, not the whole 10th score object
Glosmana at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 7
Well, once you've got the 10th score object, you should be able to figure out how to extract the score from it, shouldn't you?
dcmintera at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 8
Thats the bit I dont know how to do. I still can't get the 10th object though.int i = 9; String tenthScore = (Score)players.get(i);System.out.println(tenthScore);
Glosmana at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 9
int i = 9; String tenthScore = ((Score)players.get(i)).toString();System.out.println(tenthScore);
java_2006a at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 10
you can also make your Score object extend Object class like that : class Score extends Object ....
java_2006a at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...
# 11

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.

dcmintera at 2007-7-14 21:01:15 > top of Java-index,Java Essentials,Java Programming...