So far this is my problematic code, assume everything else is made and done:
public GameRecord[] updateHighScoreRecords(GameRecord[] highScoreRecords, String name, int level, int score) {
// write your code after this line
int i = 0;
for (GameRecord gr : highScoreRecords){
if (gr.getScore() >= gr.getScore()){
highScoreRecords.add(i+(gr.getLevel()-level),(Object) new GameRecord(name, level, score)); /*
*adds the new GameRecord in at the (i+gr's level - level)th iteration.
*note it does this because of the assumtion that highScoreRecords is ordered becuase of only using this function
*/
break; //no more need to continue the loop
}
i += 1;
}
return highScoreRecords;
}
as you may have noticed, my code is part of a course, so that is why I'm assuming all other implementations are perfect.
You are passing in a GameRecord[] highScoreRecords
array,
but calling a List method add
- this does not exist on an Array
. You should be getting a compile error.
If you are sure that the array has capacity for insertion then you could do
highScoreRecords[i+(gr.getLevel()-level)] = new GameRecord(name, level, score);
but I guess you would be better off using a List
like ArrayList
, and keeping your existing code. For this you will to pass a List to the method not an Array.
Java arrays are not dynamic data structures,
highScoreRecords.add(i+(gr.getLevel()-level),
(Object) new GameRecord(name, level, score));
I think you wanted
// Using a List.
GameRecord[] updateHighScoreRecords(List<GameRecord> highScoreRecords,
String name, int level, int score) {
Also, don't cast to Object
. That's raw-type ing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.