我怎么能新项目添加到字符串数组? [英] How can I add new item to the String array?

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

问题描述


  

可能重复:结果
  如何将新的元素添加到一个String []数组?


我如何添加新项String数组?我试图项目添加到最初空字符串。例如:

 一个字符串[];  a.add(KK);
  a.add(PP);


解决方案


  

数组是保存单个类型的值的一个固定数目的容器对象。创建阵列时的阵列的长度被确定。创建后,它的长度是固定的。你见过一个数组的例子已经在主要方法的Hello World!应用。本节讨论更详细的数组。


因此​​,在String数组的情况下,一旦你创建的一些长那么你就不能修改它。喜欢。
但是你可以到它的长度加元。

 的String [] =改编新的String [10]; // 10是阵列的长度。
改编[0] =KK;
改编[1] =PP;
...

所以,如果你的要求是增加了许多对象,其推荐使用表像:

 列表<串GT;一个=新的ArrayList<串GT;();
a.add(KK);
a.add(PP);

Possible Duplicate:
how to add new elements to a String[] array?

How can I add new item to the String array ? I am trying to add item to the initially empty String. Example :

  String a [];

  a.add("kk" );
  a.add("pp");

解决方案

From arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.

So in case of String array once you create is with some length then you can't modify it. Like. But you can add element till its length.

String[] arr = new String[10]; // 10 is the length of array.
arr[0] = "kk";
arr[1] = "pp";
...

So if your requirement is to add many object its recommended to use List's Like :

List<String> a = new ArrayList<String>();
a.add("kk");
a.add("pp"); 

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

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