如何在对象列表中增加整数字段

时间:2018-11-30 09:09:30

标签: java

好吧,我有List<HighscoreEntry>,您的课程是这样的:

public class HighscoreEntry {
    private List<String> users;
    private int score;

    HighscoreEntry(List<String> users, int score) {
        this.users = users;
        this.score = score;
    }

    public List<String> getUsers() {
        return users;
    }

    public void setUsers(List<String> users) {
        this.users = users;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

发生了什么事,是因为在另一堂课中我有这个:

public class ScoreboardItemData {
private final static HighscoreComparator comparator = new HighscoreComparator();
private final List<HighscoreEntry> entries;
private final Map<String, Integer> points;
private int scoreType;
private int clearType;

public ScoreboardItemData(int scoreType, int clearType, List<HighscoreEntry> entries, Map<String, Integer> points) {
    this.scoreType = scoreType;
    this.clearType = clearType;
    this.entries = entries;

    this.points = points;
}

public List<HighscoreEntry> getEntries() {
    return this.entries;
}

public Map<String, Integer> getPoints() {
    return this.points;
}

public void addEntry(List<String> users, int score) {
    synchronized (this.entries) {
        this.entries.add(new HighscoreEntry(users, score));

        this.entries.sort(comparator);
    }
} }

好吧,我已经可以添加新条目,但是我想更改仅得分并将其增加到现有数据。

我尝试了一些尝试,但是没有成功:

示例1

synchronized (this.entries) {
        for (int i = 0;i < this.entries.size(); i++) {
            if (this.entries.get(i).getUsers() == users) {
                this.entries.get(i).increaseScore(score);
                this.entries.sort(comparator);
            } else {
                this.entries.add(new HighscoreEntry(users, score));
                this.entries.sort(comparator);
            }
        }
    }

此示例更新了得分,但是通过循环,他将添加更多条目并更新其他条目,因此将始终执行此循环。

示例2

HighscoreEntry entry = (HighscoreEntry) this.entries.stream().filter((x) -> {
        return x.getUsers().equals(users);
    });
    if (entry.getUsers() != null) {
        entry.increaseScore(score);
    } else {
        entry.setUsers(users);
        entry.setScore(score);
    }

然后,返回此错误:

java.lang.ClassCastException: java.util.stream.ReferencePipeline$2 cannot be cast to com.heraproject.wired.data.ScoreboardItemData$HighscoreEntry
at com.heraproject.wired.data.ScoreboardItemData.addEntry(ScoreboardItemData.java:45)

3 个答案:

答案 0 :(得分:0)

如果我理解正确,那么您所需要的就是这样

    // int index, someInt;
    this.entries.get(index).setScore(someInt);

但是,查看您的模型,我认为“高分”将通过迭代潜在User对象的列表来计算,每个对象都有自己的独特得分。然后,您对用户而不是持有用户列表的实体设置分数。

答案 1 :(得分:0)

好吧

使用此方法

<canvas></canvas>
<script src="main.js"></script>

答案 2 :(得分:0)

您需要根据标识符在列表中找到正确的项目,然后查看标识符为users的HighscoreEntry。如果存在,此代码将更新列表条目中HighscoreEntry的分数

public void addScore(List<String> users, int score) {
    HighscoreEntry entry = entries.filter( e -> (e.getUsers().equals(users))).findFirst().orElse(null);
    if (entry != null) {
        entry.setScore(score);
    } 
}