如何增加数组的长度 [英] How to increase an array's length
问题描述
我有一个简单的问题.我在 java 中有一个整数数组,它需要它的长度在整个类中变化.具体来说,我需要在某些情况下加一.我是这样试的.
I have a quick question. I have an integer array in java that needs its length to vary throughout the class. Specifically, I need it to increase by one in certain situations. I tried it like this.
sbgHeadX = new int[ numberOfSBG ];
我会在需要时增加整数变量 numberOfSBG,但我认为这行不通.还有其他办法吗?
I would increase the integer variable numberOfSBG when I needed to, but I don't think this works. Is there any other way?
推荐答案
我建议你使用 ArrayList 因为您不必再担心长度了.一旦创建,就不能修改数组大小:
I would suggest you use an ArrayList as you won't have to worry about the length anymore. Once created, you can't modify an array size:
数组是一个容器对象,它包含固定数量的单一类型的值.数组的长度是在创建数组时确定的.创建后,其长度是固定的.
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.
(来源)
这篇关于如何增加数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!