Is there an expandable list of object references in Java?

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.

Asked by: Kimberly419 | Posted: 28-01-2022

Answer 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];

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.

Answered by: Stella347 | Posted: 01-03-2022

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

Answered by: Kelvin141 | Posted: 01-03-2022

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

Answered by: Lenny134 | Posted: 01-03-2022

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

Answered by: Elise964 | Posted: 01-03-2022

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

Answered by: Brad788 | Posted: 01-03-2022

Answer 6

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

Answered by: Kate442 | Posted: 01-03-2022

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

Answered by: Sam655 | Posted: 01-03-2022

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

Answered by: Max118 | Posted: 01-03-2022

Similar questions

java - Image icon in expandable list view in android

I want to add an image icon in expandable list view .I have seen the tutorial they have added only in child elements .Is there any other way to add image icon in parent Any help would be appreciated. Thanks in advance.

java - How to update expandable list view when adapter's data changes

I have an activity that extends ExpandableListActivity. I use SimpleCursorTreeAdapter to fill ExpandableListView. My layout contains list view and empty view. On app start ExpandableListActivity automatically chooses the right view to display. My steps: App starts, there is no data. (empty view on the screen) Insert some data into db. Call adapter.notifyDataSetChanged(); But empty v...

java - Expandable listview Refreshing

I am using expandable listview i need to refresh as i am searching the list only for parent.I have searched for tutorial in which i can set visibility only for some parent but not all the expandable listview object . Thanks in advance.

java - Why aren't arrays expandable?

When we create an array, we cannot change its size; it's fixed. OK, seems nice, we can create a new bigger array and copy the values one by one and that's little slow. What's the technical background of it?

How to make an expandable list with Java Swing

I need to make an expandable list using java swing. I will attempt to demonstrate: Unexpanded: >[Expand me!] >[And me!] Expanded: |[Expand me!] >[Expand us too!] >[Expand us too!] >[Expand us too!] >[And me!] So, when you click on the "Expand me" portion of the list, another lists will drop down, possibly contai...

java - Changing Expandable ListView default icons

I'm trying to change the default minimized and maximized icons for an expandable listview. I followed this tutorial closely, but I keep getting the following error in the expander_group.xml file: error: Error: No resource f...

java - Dynamic layout with expandable cells

I'm really stuck with a layout I have to make with java swing. I have to build a dynamic form, while iterating through a vector of labels and input components. Here is how it should: In this image, you can see, what should be the result. Between the green lines is one pair of label/input component. I also get...

java - Android Expandable List from Sqlite

Are there any simple tutorials to help create an expandable list from sqlite data? I have a table called categories, then a table called questions. If a category is clicked then the questions for that category need to be shown. How would I pass the category id to the second cursor? Any help appreciated.

java - Android Expandable ListView Icon (Group Indicator)

I have been able to successfully swap in my own image for the expandable listview arrows. I have two issues at the moment. One is that the icons i substitue in with the code below are each strechted to the height of the row and too wide as well. The second is, that this code is only changing the initial state of the Group Indicator. How do I change it from one image to another when a particular row is open?

java - Expandable Listview Group Indicator half off screen?

I am using a custom Group indicator as shown in the code below. Now if I leave the default I can use setBounds perfectly, but when using my custom image which is the same dimensions and setup as the same .9 patch file it is always half off screen. No matter what values I use for setBounds() Drawable plus = (Drawable) getResources().getDrawable(R.drawable.expander_group); getExpandabl...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)