在一个Android应用程序中,我制作了一个我创建的某个类的实例的数组,然后在该程序中,我需要使用该类中的getter和setter方法来处理该数组中的类的实例。我是否需要将数组中的类的实例分配给新的类初始化程序?下面是一些代码来清除此设置:如何将数组中的类的实例分配给java中的类初始化程序?
类
public class ProfileInformation {
private String console;
private String gamertag;
public String getConsole() {
return console;
}
public void setConsole(String console) {
this.console = console;
}
public String getGamertag() {
return gamertag;
}
public void setGamertag(String gamertag) {
this.gamertag = gamertag;
}
}
阵列
ArrayList<ProfileInformation> ProfTags = new ArrayList<>();
ProfileInformation的一些实例,然后添加到ArrayList中,然后我得到的一个来自arraylist的实例并尝试使用getGamertag()将其设置为字符串:
ProfileInformation profNew = ProfTags.get(ProfTags.size()-1);
String example = profNew.getGamertag();
问题是例子将等于null。为什么是这样?
向我们展示如何添加东西? –
此外,这不是特定于Android的 – SuperStormer
对于愚蠢的问题抱歉,但你真的设置“gamertag”值吗? – FonzTech