2012-03-07 87 views
0

我來自一個動作腳本,我對如何在java中使用數組感到困惑。如何構建數組,推送給它們,並在Java/Android中循環遍歷它們

在我的主要活動我創建了一個名爲mIcons空數組,像這樣

private Array mIcons; 

現在我想通過使用從我DataBaseHelper類的方法,它看起來像這樣來設置數組的值:

public Array getHomeScreenIcons() { 

     Array iconList; 

     // Select All Query 
     String selectQuery = "SELECT * FROM " + homeIcons; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 

iconList.push(Integer.parseInt(cursor.getString(0)));

  } while (cursor.moveToNext()); 
     } 
     Log.v(TAG, "List Created"); 
     // return contact list 

} 

大膽線跳出的代碼是問題至今..我怎麼推到我的數組

再後來我將要運行一個for循環從數組我主要活動using.length

+0

瞭解原始語言結構的最好方法是通過一本書或在線教程。這個問題涉及這樣的基本語法,它不屬於SO。 – 2012-03-07 20:43:33

回答

7

使用的的ArrayList paramaterized任何類型你想

ArrayList<Integer> iconList = new ArrayList<Integer>();

添加具有

iconList.add(Integer.parseInt(cursor.getString(0)));

迭代與

for (int i: iconList) 
{ 
    // i is your entire array starting at index 0 
} 

for (int i=0; i< iconList.size(), i++) 
{ 

} 
+1

這個答案大部分都是正確的,除了你不能將基元作爲集合類型傳遞。你需要'ArrayList iconList = new ArrayList ()'。 – Devunwired 2012-03-07 20:43:45

+0

謝謝你滑過我的腦海,編輯我的回答 – bbedward 2012-03-07 20:46:07

+0

哇,好吧,首先謝謝你,其次,你能向我解釋一下Array和ArrayList之間的區別嗎?我正在查找它,顯然Array必須有一個預定義的長度?很顯然ArrayList是我想要使用的,但是如果有人能向我解釋爲什麼Array的必須有預定義的長度,我會愛上你......對我來說似乎是絕對遲鈍的? – erik 2012-03-07 20:51:19

2

你可能通過

0123思考的ArrayList

private ArrayList<String> iconList = ArrayList<String>(); 
iconList.add("Stuff"); 

再後來循環

for (int i=0; i<iconList.size(); i++) { 
    String newStuff = iconList.get(i); 
} 
0

您可以用Java定義數組是這樣的:

int[] intArray = new int[3]; // array for three ints 
String[] stringArray = new String[10]; // array for 10 Strings 

因此,對於你的代碼,你可以做這樣的事情:

if (cursor.moveToFirst()) { 
    int[] iconList = new int[cursor.getCount()]; 
    int index = 0; 
    do { 
     iconList[index] = Integer.parseInt(cursor.getString(0)); 
     index++; 
    } while (cursor.moveToNext()); 
} 

之後,你可以像這樣遍歷數組:

for (int icon : iconList) { 
    // Do something with icon 
} 
1

你或許應該打了一些基本的Java教程習慣數組語法和功能。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html可能是一個很好的起點。

看你的具體問題 -

通常你不想使用Array類的,你做的方式。它更像是一個幫手類。另外,看起來你正在使用堆棧語義,而且你可能希望使用堆棧。

首先,數組: 聲明數組,像這樣:

Type[] myArray = new Type[arraySize]; 

,然後你用索引來訪問它像這樣:

Type myThingFromArray = myArray[myArrayIndex]; 

,你把東西在裏面,像這樣:

在Java中
myArray[myTargetIndex] = myObjectOfTypeType; 

原始陣列具有靜態大小並且不易可增長。對於大多數應用程序來說,使用Collections框架的成員會更好。如果您正在積極尋找堆棧(如您所說的那樣),那麼您可以使用Stack<Integer>並且具有所有常規的堆棧操作。

使用現代集合類的另一個好處是您可以使用for-each構造來遍歷集合,從而消除了一些常見的樣板。一個例子:

public ArrayList<Integer> iconList; 
public Array getHomeScreenIcons() { 

    Array iconList = new ArrayList<Integer>(); 

    // Select All Query 
    String selectQuery = "SELECT * FROM " + homeIcons; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      iconList.add(Integer.parseInt(cursor.getString(0))); 
     } while (cursor.moveToNext()); 
    } 
    Log.v(TAG, "List Created"); 
    // return contact list 

    //Iterate like so: 
    for (Integer i : iconList){ 
     System.out.println("Here's all integers in the icon-list: " + i); 
    } 
}  
相關問題