创建数组不宣的大小 - java的 [英] creating array without declaring the size - java
本文介绍了创建数组不宣的大小 - java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经四处同一个问题,但我找不到一样,我不得不
i've digging around about the same issue but i couldn't find the same as i had
我想不宣的大小来创建一个数组,因为我不知道怎么会!
i want to create an array without declaring the size because i don't know how it will be !
要清楚,我想给你code,我是仰视的问题
to clear the issue i'd like to give you the code that i'm looking up for
public class t
{
private int x[];
private int counter=0;
public void add(int num)
{
this.x[this.counter] = num;
this.counter++;
}
}
如您所见,用户可以使用添加
功能元素添加到阵列10000次或一次所以它的大小未知
as you see the user could use the add
function to add element to the array 10000 times or only once so it's unknown size
推荐答案
使用的java.util.ArrayList或LinkedList的是这样做的常用方法。使用数组这是不可能的,因为我知道了。
Using Java.util.ArrayList or LinkedList is the usual way of doing this. With arrays that's not possible as I know.
例如:
List<Float> unindexedVectors = new ArrayList<Float>();
unindexedVectors.add(2.22f);
unindexedVectors.get(2);
这篇关于创建数组不宣的大小 - java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文