如何动态地添加元素,以字符串数组?数组、字符串、元素、动态

由网友(顾及)分享简介:现在我有一个字符串数组测试。Right now I've a string array test.我要元素添加到这个数组里面一个for循环。但我没有得到的方式。我是新来的Java和Android。I want to add elements to this array inside a for loop.b...

现在我有一个字符串数组测试

Right now I've a string array test.

我要元素添加到这个数组里面一个for循环。 但我没有得到的方式。 我是新来的Java和Android。

I want to add elements to this array inside a for loop. but I am not getting a way. I am new to Java and android.

推荐答案

阵列在Java中有一个定义的大小,以后将无法添加或删除元素的改变(你可这里)读了一些基础知识。

Arrays in Java have a defined size, you cannot change it later by adding or removing elements (you can read some basics here).

相反,使用 列表

Instead, use a List:

ArrayList<String> mylist = new ArrayList<String>();
mylist.add(mystring); //this adds an element to the list.

当然,如果你事先知道你要多少字符串就摆在你的阵列,您可以创建一个大小的数组,并使用正确的位置上设置的元素:

Of course, if you know beforehand how many strings you are going to put in your array, you can create an array of that size and set the elements by using the correct position:

String[] myarray = new String[numberofstrings];
myarray[23] = string24; //this sets the 24'th (first index is 0) element to string24.
阅读全文

相关推荐

最新文章