2013-05-10 42 views
5

我想在兩個地方在Java中做一個數組列表,但我不知道我在做什麼錯。它說需要一個數組,但我不知道這是什麼意思,因爲我正在使用數組列表。ArrayList編譯器錯誤

這就是會被搞砸行:

static char rSpaces(String problem, int count) 
{ 
    num.add(problem.charAt(count)); 
    char no = num[count]; 
    return no; 
} 

如果這會有所幫助,這是我創造了(我已經導入它)數組列表行:

static ArrayList<Character> num = new ArrayList<Character>(); 

回答

5

num[count]是錯誤,因爲num不是數組。改爲使用num.get(count)

2

ArrayList不是數組,因此您不能在此處使用數組元素[]語法。使用get方法訪問元素。

2

您應該使用ArrayList.get來訪問ArrayList的元素。將其更改爲:

char no = num.get(count); 
0

Java arrayArrayList是不同的東西。

您可以通過使用方法size作爲訪問ArrayList的尺寸如下:

static char rSpaces(String problem, int count) 
{ 
    num.add(problem.charAt(count)); 
    char no = num.get(count); 
    return no; 
} 

如果您要訪問它作爲一個數組,你可以「出口」,它使用toArray方法如下:

... 
Character[] myArray = num.toArray(new Character[]{}) 
Character c = myArray[count]; 
... 
0

要使用[]操作num[count]訪問數組使用索引操作的元件,而在ArrayList中你需要的情況下使用get(count)方法。