Java中是否存在可扩展的对象引用列表? [英] Is there an expandable list of object references in Java?

查看:145
本文介绍了Java中是否存在可扩展的对象引用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们总是可以使用数组来存储对象引用.然后,我们有了一个ArrayList或HashTable,它们可以自动扩展以存储对象.但是,有谁知道一种具有自动扩展的对象引用数组的本地方法?

In Java, we can always use an array to store object reference. Then we have an ArrayList or HashTable which is automatically expandable to store objects. But does anyone know a native way to have an auto-expandable array of object references?

我的意思是我想知道Java API是否具有某些类,能够存储对对象的引用(但不存储像XXXList或HashTable这样的实际对象)以及自动扩展的能力./p>

What I mean is I want to know if the Java API has some class with the ability to store references to objects (but not storing the actual object like XXXList or HashTable do) AND the ability of auto-expansion.

推荐答案

Java数组根据其定义是固定大小的.如果需要自动增长,则可以使用XXXList类.

Java arrays are, by their definition, fixed size. If you need auto-growth, you use XXXList classes.

编辑-问题已得到澄清

当我刚开始学习Java时(来自C和C ++背景),这可能是使我震惊的第一件事.希望我能有所启发.

When I was first starting to learn Java (coming from a C and C++ background), this was probably one of the first things that tripped me up. Hopefully I can shed some light.

与C ++不同,Java中的对象数组不会存储对象.它们存储对象引用.

Unlike C++, Object arrays in Java do not store objects. They store object references.

在C ++中,如果声明类似以下内容:

In C++, if you declared something similar to:

String myStrings[10];

您将获得10个String对象.在这一点上,做类似println(myStrings [5] .length);的事情是完全合法的. -您将获得'0'-String的默认构造函数会创建一个长度为0的空字符串.

You would get 10 String objects. At this point, it would be perfectly legal to do something like println(myStrings[5].length); - you'd get '0' - the default constructor for String creates an empty string with length 0.

在Java中,当您构造一个新数组时,会得到一个空容器,该容器可以容纳10个String引用.所以打电话:

In Java, when you construct a new array, you get an empty container that can hold 10 String references. So the call:

String[] myStrings = new String[10];
println(myStringsp[5].length);

将引发空指针异常,因为您实际上尚未将String引用放置到数组中.

would throw a null pointer exception, because you haven't actually placed a String reference into the array yet.

如果您来自C ++背景,请考虑将新String [10]等同于C ++中的new(String *)[10].

If you are coming from a C++ background, think of new String[10] as being equivalent to new (String *)[10] from C++.

因此,考虑到这一点,应该很清楚为什么ArrayList 自动扩展对象数组的解决方案(实际上,ArrayList是使用简单的数组和增长算法实现的内置,可根据需要分配新的扩展数组,并将内容从旧的复制到新的.)

So, with that in mind, it should be fairly clear why ArrayList is the solution for an auto expanding array of objects (and in fact, ArrayList is implemented using simple arrays, with a growth algorithm built in that allocates new expanded arrays as needed and copies the content from the old to the new).

在实践中,实际上,我们使用数组的情况相对较少.如果您正在编写一个容器(类似于ArrayList或BTree),则它们很有用,或者您正在执行许多低级字节操作-但在大多数开发发生的级别,使用Collections类之一到目前为止是首选技术.

In practice, there are actually relatively few situations where we use arrays. If you are writing a container (something akin to ArrayList, or a BTree), then they are useful, or if you are doing a lot of low level byte manipulation - but at the level that most development occurs, using one of the Collections classes is by far the preferred technique.

这篇关于Java中是否存在可扩展的对象引用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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