views:

1255

answers:

8

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.

A: 

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

Jack Leow
A: 

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

anjanb
it's Java, not Javascript
Jason Cohen
This is the strangest answer that I've read in quite a while.
jsight
Hey guys, take it easy. Java 6 comes with a Javascript engine. If one so wants to do a certain thing, then they can do it using Rhino javascript engine.Then, again, I should have mentioned that the user ccould use the Rhino JS engine to do this.
anjanb
+4  A: 

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.

Kevin Day
+1  A: 

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.

Vincent Ramdhanie
A: 

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.

Brandon DuRette
+1  A: 

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.

outsyncof
+4  A: 

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

PhiLho
+1  A: 

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.

mtruesdell