For循环填充数组? [英] For Loop to populate Array?
问题描述
在ImageAdapter类本教程中,为http://开发商。 android.com/resources/tutorials/views/hello-gridview.html
In the ImageAdapter class of this tutorial, http://developer.android.com/resources/tutorials/views/hello-gridview.html
我想创建和填充使用for循环的数组。但似乎不管我在哪里把它,它会导致错误。
I would like to create and populate an array using a for loop. But it seems no matter where I place it, it causes an error.
例如,在私人语境mContext;
我把下面的,它导致错误。我认为循环是好的,我只是不知道在那里我可以把它。结果
For example, under private Context mContext;
I put in the following and it causes an error. I think the loop is good, I'm just not sure where I can put it.
私有String [] myString的;
对于(INT数= 0;&号LT; = 12;数++){
MyString的[数字] =形象+号;
}
推荐答案
创建和填充在构造函数中的阵列。不要忘了实际上的实例化数组你开始填充它。
Create and populate the array in the constructor. Don't forget to actually instantiate the array before you start populating it.
public ImageAdapter(Context c) {
mContext = c;
myString = new String[12]; //create array
for (int number = 0; number < myString.length; number++) {
myString[number] = "image" + number;
}
}
您或许应该直接跃入前的Android了一下你的Java工作。
You should perhaps work on your Java a bit before jumping straight into Android.
这篇关于For循环填充数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!