将对象添加到arraylist是否会将对象的变量设置为null?

时间:2018-12-27 18:55:03

标签: java arraylist

我正在为Android创建一个用于跟踪团队统计信息的应用程序,并且正在使用ArrayList存储团队列表。为什么对象不将其字段保存在ArrayList中?

我已经尝试过在列表中创建一个新团队,然后将其设置为与旧团队相同。 (teamList.add(new TeamStats()); teamList.get(teamList.size()-1) = team;),但这无法运行并引发错误。

public class TeamStats implements Serializable {
    //General Info
    public String TeamName;
    public String School;
    public boolean needUpdate;
    //...
}   


ArrayList <TeamStats> teamList = new ArrayList<TeamStats>();

Intent intent = getIntent();
TeamStats team = (TeamStats) intent.getSerializableExtra("Team");

if (team != null && team.needUpdate == true){
    System.out.println("Name before list: " + team.TeamName);
    teamList.add(team);
    System.out.println("Name after list: " + 
        teamList.get(teamList.size()-1).TeamName);
    System.out.println("Same object? " + (team == teamList.get(teamList.size()-1)));
}

//iterates through teamList
for (int i = 0; i < teamList.size(); i++){
        System.out.println(teamList.get(i).TeamName);
    }

我希望输出为:

Name before list: Alpha
Name after list: Alpha
Same object? true
Theta
Beta
Alpha

但是我得到的结果是:

Name before list: Alpha
Name after list: null
Same object? true
Theta
Beta
Null

0 个答案:

没有答案