尝试访问数组java中的元素时表达式预期错误

时间:2015-09-20 11:57:24

标签: java android arraylist static compiler-errors

我用静态方法创建了一个类,我正在尝试访问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”。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

AllLanguages是一个不是数组的类,因此AllLanguages[position]是无效的语法。

您需要更改

public Language getLanguage(int position) {
    return AllLanguages[position];
}

public Language getLanguage(int position) {
    return mLanguages[position];
}