且构网

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

如何在Java中增加数组的大小?

更新时间:2023-10-02 22:17:58

不要使用数组,而是使用 java.util.List 的实现,例如 ArrayList.ArrayList 有一个数组后端,它保存列表中的值,但数组大小由列表自动处理.

Instead of using an array, use an implementation of java.util.List such as ArrayList. An ArrayList has an array backend which holds values in a list, but the array size is automatically handles by the list.

ArrayList<String> list = new ArrayList<String>();
list.add("some string");

对于其他元素类型,您还可以使用 list.toArray(new String[list.size()]) 等将列表转换为数组.

You can also convert the list into an array using list.toArray(new String[list.size()]) and so forth for other element types.