简体   繁体   English

链表 <type> 充满重复的条目

[英]LinkedList<type> filled with repeat entries

I start with a table from MySQL filled with player data for a scoreboard. 我从MySQL的一张表开始,里面充满了记分牌的玩家数据。 It has 4 entries with different names and scores. 它具有4个具有不同名称和分数的条目。 My code sucessfully retrieves these entries. 我的代码成功检索了这些条目。 I tested this as I retrieved the entries using this method: 我使用此方法检索条目时对此进行了测试:

while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int score = resultSet.getInt("score");
            int lastScore = resultSet.getInt("lastScore");
            System.out.println("" + Integer.toString(id) + ", " + name + ", $" + Integer.toString(score) + ".");
            Scoreboard.addScore(id, name, score);

The println() returns the following four lines: println()返回以下四行:

1, Sam, $10000.
2, Jesus, $12000.
3, Michael, $9000.
4, Asako, $4500.

addScore() looks like this: addScore()看起来像这样:

    public static void addScore(int id, String name, int score){//Adds new score to scores LinkedList
        if (scores.isEmpty())
            scores.addFirst(new Player(id, name, score, 0));
        for (int i = 0; i < scores.size(); i++){
            if (score > scores.get(i).getScore()){
                scores.add(i, new Player(id, name, score, 0));
                return;
            }
        }
        scores.addLast(new Player(id, name, score, 0));
    }

Finally, the Player class looks something like this: 最后,Player类如下所示:

public class Player {
    private static int id;
    private static String name;
    private static int score;
    private static int lastPosition;

    public Player(int id, String name, int score, int lastPosition){
        setID(id);
        setName(name);
        setScore(score);
        setLastPosition(lastPosition);
    }
//...

When I finally get around to displaying the scores it displays this: 当我最终显示分数时,它将显示以下内容:

  1. Asako $4500 浅草$ 4500
  2. Asako $4500 浅草$ 4500
  3. Asako $4500 浅草$ 4500
  4. Asako $4500 浅草$ 4500

Is there anything wrong with my code that could be causing this? 我的代码有什么错误可能导致这种情况吗? Also, is there any code that you think could be necessary to better help you assess this question? 另外,您是否认为有必要使用任何代码来更好地帮助您评估此问题? I tried to put all of the significant code in. 我试图放入所有重要的代码。


While retrieving the scoreboard I use this code scores.get(i).setLastPosition(i+1); 在检索计分板时,我使用以下代码scores.get(i).setLastPosition(i+1);

Is that correct syntax? 那是正确的语法吗? Can I set a value of part of the element in the LinkedList by calling get, or is this not possible? 是否可以通过调用get来设置LinkedList中元素的一部分的值,或者这不可能吗?

You class Player defines 4 static variables, which means they are defined once for the entire class, no matter how many instances of Player you create. 您为Player类定义了4个static变量,这意味着无论您创建多少个Player实例,整个类都将定义一次。 Each new instance overwrites the values, and the last one, Asako $4500 , "wins". 每个新实例将覆盖值,最后一个实例Asako $4500 “获胜”。

Remove static from the 4 variables in Player , so that they have one value for each instance of the Player class. Player的4个变量中删除static变量,以使它们对于Player类的每个实例都具有一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM