将字符串添加到String数组 [英] add string to String array

查看:180
本文介绍了将字符串添加到String数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何将新元素添加到String []数组?

我是Java新手,所以我需要一点帮助

I am new in Java so I need little help

我有

String [] scripts = new String [] ("test3","test4","test5");

我想在这个数组(脚本)中添加新字符串(string1,string2)以供示例

and I want to add new strings ( string1,string2) to this array ( scripts) for examples

String string1= " test1"
String string2 = "test2"

我想在init中添加新字符串但在后期阶段

I want to add the new string not in the init but in later phase

我如何可以做到吗?

推荐答案

你无法在java中调整数组大小。

You cannot resize an array in java.

一旦声明了数组的大小,它就会保持不变。

Once the size of array is declared, it remains fixed.

相反,你可以使用具有动态的 ArrayList 大小,这意味着你不必担心它的大小。如果您的数组列表不足以容纳新值,那么它将自动调整大小。

Instead you can use ArrayList that has dynamic size, meaning you don't need to worry about its size. If your array list is not big enough to accommodate new values then it will be resized automatically.

ArrayList<String> ar = new ArrayList<String>();
String s1 ="Test1";
String s2 ="Test2";
String s3 ="Test3";
ar.add(s1);
ar.add(s2);
ar.add(s3);

String s4 ="Test4";
ar.add(s4);

这篇关于将字符串添加到String数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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