Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Java Arrays?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi everyone!
I've got a question for you. Is there the same mechanism in Java for creating dynamic arrays as there is in Visual Basic?

I don't know in advance how many elements there going to be in array, and I need to resize array dynamically along with adding new elements to it.

Something like in this piece:


int[] arr;
int count;
int arrelem=0;

for (count=0;count<5;count++)
arr[count]=arrelem;
arrelem++;


Java doesn't seem to let to use this approach. It insists on initializing array with specifying the expected number of elements. So, is this a dead end, or there is some workaround?

Thank you in advance.


 
Hi,

There are workarounds. You can take a look at Vectors or ArrayLists. You don't have to specify the size while declaring or initialising them. You can add to the ArrayList as many objects as you like.

Depending on how your coding works too, sometimes it is possible to declare and initialise your array after you know the size that you want to declare it as.

Regards,
Leon
 
The only problem with a Vector is that it will only store objects, so if you need a dynamic array of ints for example, you will need to use the Integer wrapper class.

 
LeonTang & pipk,
thnx for your responses. I'm going to try your suggestions.
Take care.
guestg.
 
FYI, here is sample code for using an arraylist:

ArrayList list = new ArrayList();
Integer[] intArray;
for(....)
{
list.add(whatever you want to add)
}
intArray = (Integer[]) list.toArray(new Integer[list.size()])

you could probably just as easily use a primitive int[] array as an Integer[] array.
 
using your code, LeonTang suggestion is best for your purposes as you want a primitive int[]

given this:

int[] arr;
int count;
int arrelem=0;

for (count=0;count<5;count++)
arr[count]=arrelem;
arrelem++;

you have hard coded in the 5.
This value has to be known before you enter the for loop, lets call this MAX_LENGTH, therefore do the following:

// Stage 1, declare your int[]
int[] myArray;

// Stage2, initialise it
myArray = new int[ MAX_LENGTH + 1 ];

// Now go through your for loop
for( ... )
{
}

you should remember, an array, like all primitives and objects goes through stages of use i.e. declaration, initialisation and then use. When you initialise the array is up to you. this does not have to be done the minute you declare the array.

sure, you can us evectors and the like but then you have to case objects left, right and centre...don't bother if this is good enough for your purposes.

you were asking this question in relation to VB, probably the redim command. you should write one of these for yourself. it wouldn't be very difficult and would be good practice for you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top