• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Array resizing

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you dynamically resize an array? I'd like to declare it with an initial size, but add to its size if I go out of bounds.
So I'd start with:
String sStuff[] = new String[100];
to get the initial size - is there a way to up the size by, say, 10 elements a time if I need to?
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer here, Ben, is "no". You cannot dynamically resize an array.
But why not look into <pre>java.util.Vector</pre> instead? Read all about it.
HTH
Art
[This message has been edited by Art Metzer (edited April 04, 2001).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write a method that will expand the array (by cheating) but it is clumsy and somewhat backwards when compared to Vector. But here ya go anywho:

Good luck!!
Michael
 
Ben Hodgson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent - I liked both of those. I'm happy using Vectors, so I'll probably stick with them. Good to find out about the array thing (and the work around), so thanks for the answers, very much appreciated.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, however, that arrays typically perform better than Vectors, if that is a consideration later on.
And, as Peter Haggar points out in his book Practical Java, there is a System class in the java.lang package with a method called
arraycopy(Obj src, int src_pos, Obj dest, int dest_pos, int length);
It takes a sorce array and a destination array, and using starting positions and a length, to quickly copy an array (nearly twice as fast as a for loop).
The code would look like this:

Apparently this is faster because the method is implemented as a native method, which can more directly and efficiently transfer the contents in memory from source to destination.
However, the arraycopy method has a number of gotchas to watch out for if you look in the API when you go beyond simple copying like above.
OP
 
It runs on an internal combustion engine. This ad does not:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic