This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Setting the size of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 1815
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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Setting the size of an array
 
Similar Threads
MultiDimensional Arrays
Array lengths
array size?
Array initialization
Learning Project 2