我用静态方法创建了一个类,并尝试访问AllLanguages数组中的元素
public final class AllLanguages {
private static Language[] mLanguages;
private AllLanguages(Language[] languages) {
mLanguages = new Language[]
{
new Language(R.drawable.united_kingdom, "English", "en"),
new Language(R.drawable.france, "French", "fr"),
new Language(R.drawable.israel, "Hebrew", "he"),
new Language(R.drawable.germany, "German", "de")
};
}
public static Language[] getLanguages() {
return mLanguages;
}
public static void setLanguages(Language[] mLanguages) {
AllLanguages.mLanguages = mLanguages;
}
public Language getLanguage(int position) {
return AllLanguages[position];
}
}
在最后一个方法(getLanguage)中,我遇到以下编译器错误“Expression Expected”。有任何想法吗?
最佳答案
AllLanguages
是一个类,而不是数组,因此AllLanguages[position]
是无效的语法。
你需要改变
public Language getLanguage(int position) {
return AllLanguages[position];
}
至
public Language getLanguage(int position) {
return mLanguages[position];
}