2011-05-30 127 views

回答

3

如果你想要動態大小然後使用arraylist。如:

public ArrayList<String> myList = new ArrayList<String>(); 

... 

myList.add("blah"); 

... 

for(int i = 0, l = myList.size(); i < l; i++) { 
    // do stuff with array items 
    Log.d("myapp", "item: " + myList.get(i)); 
} 
+0

非常感謝您的幫助! :) – Kris 2011-05-30 11:04:05

2

用途爲:

public String[] videoNames = new String[SIZE]; // SIZE is an integer 

或使用ArrayList爲可調整大小的數組實現。


編輯: 並初始化它像這樣:

int len = videoNames.length(); 
for(int idx = 0; idx < len; idx++) { 
    videoNames[idx] = ""; 
} 

隨着ArrayList

ArrayList<String> videoNames = new ArrayList<String>(); 
// and insert 
videoNames.add("my video"); 
+0

@約翰您好,感謝您的答覆。但是,我怎樣才能使用數組列表,我怎麼才能初始化一個數組正確當我初始化數組爲空,我的代碼返回nullPointerException ...對於noob問題 – Kris 2011-05-30 10:46:33

+0

有沒有辦法指定無限數組大小? – Kris 2011-05-30 10:52:12

+0

@Kris:然後使用'ArrayList'類。 – 2011-05-30 10:53:16

2

你應該寫這樣的。

String[] videoNames = new String[5]; // create videoNames array with length = 5 
for(int i=0;i<videoNames.length();i++) 
{ 
    // IMPORTANT : for each videoName, instantiate as new String. 
    videoNames[i] = new String(""); 
} 
1

如果你想使用ArrayList寫在你的CommNet這裏是一個ArrayList

ArrayList<String> videoNames =new ArrayList<String>(); 

,只要你想沒有必要給大小添加儘可能多

videoNames.add("yourstring"); 
videoNames.add("yourstring"); 
videoNames.add("yourstring"); 
videoNames.add("yourstring"); 

to清空列表

videoNames.clear(); 

得到一個字符串使用

String a=videoNames.get(2); 

2是你的字符串索引