如何动态地添加元素,以字符串数组? [英] How to dynamically add elements to String array?

查看:274
本文介绍了如何动态地添加元素,以字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于如何动态地添加元素,以字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆