如何在Java中以编程方式初始化1000个字符串?

时间:2013-08-22 18:44:31

标签: java string for-loop

我想在ma app list wise中初始化1000个字符串

public static final String[] IMAGES = {"1","2","3","4","5","6",......"1000"};

3 个答案:

答案 0 :(得分:4)

不确定

public static final String[] IMAGES;
static {
    IMAGES = new String[1000];
    for (int i = 1; i <= IMAGES.length; ++i) {
        IMAGES[i - 1] = String.valueOf(i);
    }
}

答案 1 :(得分:1)

如果字符串本身只是表示其在数组中的位置的整数,则创建数组并运行for循环。

String[] IMAGES = new String[1000];

for(int x = 0; x < IMAGES.length; x++){
    IMAGES[x] = Integer.toString(x);
}

答案 2 :(得分:1)

可能是这样的:

public static final String[] IMAGES;
static {
    IMAGES = new String[1000];
    for (int i = 0; i < IMAGES.length; i++) {
        IMAGES[i] = String.valueOf(i+1);
    }
}