aspose file tools
The moose likes Java in General and the fly likes any way arrays in java can be extended dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "any way arrays in java can be extended dynamically?" Watch "any way arrays in java can be extended dynamically?" New topic
Author

any way arrays in java can be extended dynamically?

siva venkata prasad
Greenhorn

Joined: Jul 14, 2011
Posts: 17
can arrays in java can be extended dynamically? I mean i declared size of an array but I want to add more elements that declared.. please advise.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

There is no Java language command to do that but the Java collection classes allow one to bypass the problem. If you can't use the collection classes which do this behind the scenes then you will have to do what they do and allocate a new array the length you want, populate it from the content of the original array and then replace the original with the new. This can be done with one line by using the static members of java.util.Array.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
siva venkata prasad
Greenhorn

Joined: Jul 14, 2011
Posts: 17
actually.. I was asked to implement "Queue" functionality using Arrays and with out using any API.. and emphasized on how can I make arrays dynamic with out any collection API?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9955
    
    6

write code that takes an array, finds how big it is, create a new array that is larger (by whatever size you feel necesarry), then copy the elements in to your new array, then return the reference to that new array.

Each time you try and add something to your array, you'll have to check and see if it is full or not. If so, call your method, THEN add it. If it's not full, just add it.


Never ascribe to malice that which can be adequately explained by stupidity.
Kristjan Toots
Ranch Hand

Joined: Jun 03, 2011
Posts: 59

I'll add some to the James Sabre excellent explanation.
Use the arraycopy method to efficiently copy data from one array to another


Please correct my english.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

siva venkata prasad wrote: I was asked to implement "Queue" functionality using Arrays and with out using any API..

is that home work? come up with your own idea, then surely we are with you
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

Hello Siva,
Remember that in java arrays are static in nature. once we created an array, we are not supposed to increase or decrease the size. that is, of course, the drawback of arrays. To achieve this thing only we go for collection frame work, where you can use Arrays class, so that you can add more elements to that as the collection classes are dynamic in nature. We don't bother about the size in collections


Regards,

RK
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
You could do that with a circular array. You have input and retrieval indices. Whenever anything is "offer"ed to the Queue, you increment your input index. If your input index reaches the size of the array, you use the % operator to bring it back to the beginning. If your input index catches up with the retrieval index, you can enlarge the array, or throw an Exception.
Whenever you "poll" anything, you increment the retrieval index (and assign the removed value in the array to null). If the retrieval index catches up with the input index, you throw an Exception. If the retrieval index reaches the size of the array, you use the % operator to go back to the beginning.

Note, instead of using the % operator you can use the & operator and ensure the size of the array is always an exact power of 2.
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 87
The situation with arrays in Java is that they are STATIC. In other words, you cannot change their size or holding capacity.

However, what might be good for you is to use what are called ArrayLists.

ArrayLists are similar to arrays but they can be extended or reduced in size.

To create one, just simply use the code...



If you want to put things in, you just simply type something like this:





This will place the variable take_up_space into the first space or memory, being 0.
See the main Java site for more methods on how to insert items into specific memory locations or remove them.

I do believe that you can put in multiple types of variables of different nature, but I maybe wrong about that.
If you wanted to do something like that, it may be better to use the Container class.

Hope this helps.

S.T.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: any way arrays in java can be extended dynamically?
 
Similar Threads
Calling variable or methods with Strings?
enhanced for loop question
Array Class
private constructors
Object array & primitives array