如何增加数组的长度 [英] How to increase an array's length

查看:826
本文介绍了如何增加数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速的问题。我有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屋!

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