Is there an expandable list of object references in Java?

北战南征 提交于 2019-12-11 06:29:53

问题


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?

Edit: 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.


回答1:


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

EDIT - question has been clarified a bit

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.

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

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

String myStrings[10];

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.

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);

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

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

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).

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.




回答2:


All the classes implementing Collection are expandable and store only references: you don't store objects, you create them in some data space and only manipulate references to them, until they go out of scope without reference on them.

You can put a reference to an object in two or more Collections. That's how you can have sorted hash tables and such...




回答3:


What do you mean by "native" way? If you want an expandable list f objects then you can use the ArrayList. With List collections you have the get(index) method that allows you to access objects in the list by index which gives you similar functionality to an array. Internally the ArrayList is implemented with an array and the ArrayList handles expanding it automatically for you.




回答4:


Straight from the Array Java Tutorials on the sun webpage:

-> An array is a container object that holds a fixed number of values of a single type.

Because the size of the array is declared when it is created, there is actually no way to expand it afterwards. The whole purpose of declaring an array of a certain size is to only allocate as much memory as will likely be used when the program is executed. What you could do is declare a second array that is a function based on the size of the original, copy all of the original elements into it, and then add the necessary new elements (although this isn't very 'automatic' :) ). Otherwise, as you and a few others have mentioned, the List Collections is the most efficient way to go.




回答5:


In Java, all object variables are references. So

Foo myFoo = new Foo();
Foo anotherFoo = myFoo;

means that both variables are referring to the same object, not to two separate copies. Likewise, when you put an object in a Collection, you are only storing a reference to the object. Therefore using ArrayList or similar is the correct way to have an automatically expanding piece of storage.




回答6:


There's no first-class language construct that does that that I'm aware of, if that's what you're looking for.




回答7:


It's not very efficient, but if you're just appending to an array, you can use Apache Commons ArrayUtils.add(). It returns a copy of the original array with the additional element in it.




回答8:


if you can write your code in javascript, yes, you can do that. javascript arrays are sparse arrays. it will expand whichever way you want.

you can write

a[0] = 4;
a[1000] = 434;
a[888] = "a string";



来源:https://stackoverflow.com/questions/242079/is-there-an-expandable-list-of-object-references-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!