File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting the size of an array

 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[] )
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic