aspose file tools*
The moose likes Java in General and the fly likes Dynamically increasing array size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamically increasing array size" Watch "Dynamically increasing array size" New topic
Author

Dynamically increasing array size

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi alll
Can anybody tell me how we can declare an array and increase its size dynamically.....
i.e the no of elements to be put in an array can be determined at runtime.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

Originally posted by Parameswaran Thangavel:
hi alll
Can anybody tell me how we can declare an array and increase its size dynamically.....
i.e the no of elements to be put in an array can be determined at runtime.


Once an array object is instantiated, it's size it fixed -- it can't be increased dynamically. You have two options...

  • You can instantiate another array and copy the elements to the new array.
  • Use a collection class instead.


  • Henry
    [ June 30, 2006: Message edited by: Henry Wong ]

    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    2a: use a Collections class then use toArray() to convert to an array at the fast moment before returning.
    Rue Fi
    Greenhorn

    Joined: Jul 08, 2006
    Posts: 7
    What about using Vectors?
    [ July 09, 2006: Message edited by: Rue Fi ]
    Tony Morris
    Ranch Hand

    Joined: Sep 24, 2003
    Posts: 1608
    An array is fixed size, List is poorly designed, net.tmorris.adt.sequence.Sequence fixes it all.


    Tony Morris
    Java Q&A (FAQ, Trivia)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Dynamically increasing array size