gpt4 book ai didi

java - 尝试从一个 ArrayList 到另一个 ArrayList 创建新引用是移动而不是复制我的原始对象

转载 作者:行者123 更新时间:2023-12-02 02:55:13 24 4
gpt4 key购买 nike

首先,我已经搜索过。我找到的所有答案都是关于在两个不同的数组列表中没有对同一对象的相同引用。所以也许这应该告诉我我本身就做错了?但我不确定还有什么办法可以解决这个问题。

我正在努力学习Java。其中一部分就是 Swing。

我正在开发的应用程序是一个简单的锦标赛/括号应用程序(想想疯狂游行。或任何简单的括号树)。

简化的代码,仅解决当前的问题:

class Bracket extends JPanel {
ArrayList<Round> rounds = new ArrayList<Round>();

public void declareWinner(Team team, Game game, Round round) {
// code that gets current round, game, team numbers to calculate the next round, game, team numbers.

int currentRoundNum = rounds.indexOf(round);
int currentGameNum = rounds.get(currentRoundNum).games.indexOf(game);
int currentTeamNum = rounds.get(currentRoundNum).games.get(currentGameNum).teams.indexOf(team);

int nextRoundNum = currentRoundNum + 1;
int nextGameNum = (int) (Math.floor(gameNumber / 2));; // to position the team in the appropriate game slot in the next round
int nextTeamNum = ((gameNumber % 2) == 0) ? 0 : 1; // even game numbers will be the top team in the next round, odd team numbers will be the bottom team in the next round


// here is where things are getting wonky. Trying to set the next round slot to the team that is being declared the winner
rounds.get(nextRoundNum).games.get(nextGameNum).teams.set(nextTeamNum, team);
}

public Bracket(int numRounds) {
// code that creates the bracket structure. # of rounds. the correct # of games depending on the round. etc. Creates empty shell of "placeholder" teams with no name/info.

for(int i = 0; i < numRounds; i++) {
int numGames // set to (number of games of the last round added) * 2
rounds.add(0, new Round(numGames)
}

}
}

class Round extends JPanel {
ArrayList<Game> games = new ArrayList<Game>();

public Round(int numGames) {
for(int i = 0; i < numGames; i++) {
games.add(new Game());
}
}
// more code...
}

class Game extends JPanel {
ArrayList<Team> teams = new ArrayList<Team>();
// more code... creates two teams per each game in constructor
}

class Team extends JPanel {
String teamName;
public Team(String name) {
teamName = name;
add(new JLabel(name));
}
// simple team info, just going for functionality. not much else is here yet.
}

所以,假设在第一轮(轮次索引 0)中,在第一场比赛(rounds.get(0).games 索引 0)中,Team 1(rounds.get(0).games 索引 0) .get(0).teams) 获胜。它正确地计算了所有的东西。该团队被放置在正确的位置。但是,它完全将团队从当前的轮次位置中移除。所以现在我在第一轮第一场比赛中只剩下一支球队了。

它不会让我在 Rounds[0].Games[0].Teams 和 Rounds[1].Games[0].Teams 的两个 ArrayList 中引用相同的 Team 对象。它们都是嵌套的数组列表,因此有 2 个不同的数组列表。我失败是否是因为在类本身上扩展 JComponent 不好,而我应该完全重构它?

最佳答案

我无法确切地看到您的示例代码中发生了什么,因为它不是独立的。然而,从您的描述来看,您似乎违反了 Swing 组件一次只能添加到一个容器的属性。如果将相同的组件添加到第二个容器中,它将自动从第一个容器中删除。

这丝毫不会影响 ArrayList 的内容 - 您可以根据需要在多个不同的 ArrayList 中拥有相同的对象。

看起来您还通过将此类数据存储在扩展 Swing 组件的对象中来混淆视听。我建议您考虑将数据结构(模型)与显示组件( View )分开,以使事情更清晰。首先让数据结构工作,然后在验证它正确后从中构建 View 。

在一个不相关的注释中,看起来您可以简化代码的开头,之前:

public void declareWinner(Team team, Game game, Round round) {
// code that gets current round, game, team numbers to calculate the next round, game, team numbers.

int currentRoundNum = rounds.indexOf(round);
int currentGameNum = rounds.get(currentRoundNum).games.indexOf(game);
int currentTeamNum = rounds.get(currentRoundNum).games.get(currentGameNum).teams.indexOf(team);
...

您可以将这些行替换为

public void declareWinner(Team team, Game game, Round round) {
// code that gets current round, game, team numbers to calculate the next round, game, team numbers.

int currentRoundNum = rounds.indexOf(round);
int currentGameNum = round.games.indexOf(game);
int currentTeamNum = game.teams.indexOf(team);
...

因为您已经引用了作为参数传入的那些对象。

关于java - 尝试从一个 ArrayList 到另一个 ArrayList 创建新引用是移动而不是复制我的原始对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com