2012-10-24 43 views
-2

嘿傢伙我試着arraylists,我需要做一個可變大小的整數數組。 我不知道我是否正確實施。 我該如何去做這件事?有人可以幫我解決這個問題嗎?Java:可變大小的數組int

Integer[] ints = new Integer[x]; 
    static List<Integer> ints = new ArrayList<Integer>(); 

我希望能夠做像ints.add(0)這樣的事情; 等

EDIT:代碼:

Integer[] ints = new Integer[x]; 
    static List<Integer> ints = new ArrayList<Integer>(); 
    ints.clear(); 
    for (int counter2 = 0; counter2 < 50; counter2++) { 
     if (list[counter2].userActive == true) { 
     if (list[counter2].getLastName().equals(lastName) || (lastName=="")) { 
      if ((list[counter2].getFirstName().equals(firstName))||(firstName=="") 
       && (list[counter2].userActive == true)) 
       ints.add(counter2);  
+3

我們需要查看代碼以幫助您瞭解語法。 –

+0

這是我的。 Integer [] ints = new Integer [x]; 靜態列表 ints = new ArrayList (); ints.clear(); \t \t對(INT計數器2 = 0;計數器2 <50;計數器2 ++){ \t \t \t如果(列表[計數器2] .userActive == TRUE){ \t \t \t \t如果(列表[計數器2] .getLastName()。等(lastName)||(lastName ==「」)){ \t \t \t \t \t如果((list [counter2] .getFirstName [counter2] .userActive == true))ints.add(counter2); – user1766889

+2

請更新您的OP。 –

回答

1

標準Java API不具有基元(比基準類型除外)的可變大小的陣列。有第三方庫提供此功能(GNU trove被廣泛使用;請參閱here以獲得優質庫的清單),或者您可以自行構建。

如果你不介意的裝箱/拆箱的開銷,你可以使用一個ArrayList<Integer>

List<Integer> list = new ArrayList<Integer>(); 

編輯看着你發佈的代碼,唯一錯了,我看到的是,你是宣佈ints既爲Integer[]又作爲static List<Integer>。你不能兩次聲明同一個變量。擺脫Integer[]聲明,其餘代碼看起來應該正常運行(至少是將整數值添加到可變大小列表的部分;我對你的邏輯一無所知)。