如何将新项目添加到 String 数组? [英] How can I add new item to the String array?

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

问题描述

可能的重复:
如何向 String[] 数组添加新元素?

如何向 String 数组添加新项目?我正在尝试将项目添加到最初为空的字符串中.示例:

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");

推荐答案

来自 数组

数组是一个容器对象,它包含固定数量的单一类型的值.数组的长度是在创建数组时确定的.创建后,其长度是固定的.您已经在Hello World!"的 main 方法中看到了一个数组示例.应用.本节更详细地讨论数组.

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.

所以在 String 数组的情况下,一旦创建了具有一定长度的数组,就无法对其进行修改,但是你可以添加元素直到你填满它.

So in the case of a String array, once you create it with some length, you can't modify it, but you can add elements until you fill it.

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

因此,如果您的要求是添加多个对象,建议您使用如下列表:

So if your requirement is to add many objects, it's recommended that you use Lists like:

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

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

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