在不声明大小的情况下创建数组 - java [英] creating array without declaring the size - java

查看:54
本文介绍了在不声明大小的情况下创建数组 - 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 !

为了解决这个问题,我想给你我正在寻找的代码

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++;
 }
}

如您所见,用户可以使用 add 函数将元素添加到数组 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屋!

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