如何在Java中声明动态对象数组? [英] How to declare a dynamic object array in Java?

查看:659
本文介绍了如何在Java中声明动态对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个关于Java的问题。我有一个用户定义的对象类student,它有2个数据成员,name和id。在另一个类中,我必须声明object [],(例如 student stu [?]; )。但是,我不知道对象数组的大小。是否可以声明一个对象数组,但不知道它的大小?谢谢。

I want to ask a question about Java. I have a user-defined object class, student, which have 2 data members, name and id. And in another class, I have to declare that object[], (e.g. student stu[?];). However, I don't know the size of the object array. Is it possible to declare an object array but don't know the size? thank you.

推荐答案

用户 ArrayList 。它会在您添加新元素时自动扩展。如果需要,稍后你可以将它转换为数组。

User ArrayList instead. It'll expand automatically as you add new elements. Later you can convert it to array, if you need.

作为另一个选项(不确定你想要什么),你可以声明 Object [ ] 字段,不立即初始化。

As another option (not sure what exactly you want), you can declare Object[] field and not initialize it immediately.

这篇关于如何在Java中声明动态对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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