且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将新项目添加到 String 数组?

更新时间:2023-11-28 18:08:40

来自 数组

数组是一个容器对象,它包含固定数量的单一类型的值.数组的长度是在创建数组时确定的.创建后,其长度是固定的.您已经在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");