GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Setting the size of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting the size of an array" Watch "Setting the size of an array" New topic
Author

Setting the size of an array

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hello ,
Could you please tell me how to do this. I want to have an array which is String in type which the size gets increased dynamically. Say if there are 10 items the array size is 10 . If there is only 1 item the array size is one. Like that initially with out specifying the size.
Thanks


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Probably the easiest way to do this is to use a java.util.List instead of an array. If you need to use an array, you just call .toArray() on the list to express it as an array. (Or .toArray(new SomeClass[0]) to return an array that can be cast to a SomeClass[] )


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You cannot do it with a Java built-in array (the [] kind). An approximation would be an ArrayList found in the java.util package. You could use ArrayList's trimToSize() method when you remove things from the end of the ArrayList.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting the size of an array