string数组如何赋值

string数组如何赋值

可以通过直接赋值或者使用循环遍历的方式来给字符串数组赋值。

直接赋值:

String[] array = {"Hello", "World"};

使用循环遍历赋值:

String[] array = new String[2];

array[0] = "Hello";

array[1] = "World";

动态赋值:

String[] array = new String[n];

for (int i = 0; i < n; i++) {

array[i] = "Value " + i;

}

其中,n为数组的长度,可以根据实际情况进行调整。

相关推荐