• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about expanding array

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so i have a situation where the array i need to create could be a maximum of 2^n size, but rarely does that happen. Usually it is around a third of that. So i guess my question is:Should i still just make an array of size 2^n, and intialize all elements(which i must do ) or should i just make it bigger each time i have to add something to it, since the number of elements rarely reaches the true size of it. i would have to do an array copy to copy the old array thus far into the new one, and i dont really know if i should do this or just make an array with an intial size of the mximum capacity at te beginning.

I know that your probably going to say use a vector, but my array is two dimensional, and the code im working with was someone elses which requires a 2d array and i dont want to go change all his work.
so based on that, which one is faster:intialze to max size...or to array copy and make a new one each time a new element is added?
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you thought about storing 1 dimensional arrays in an ArrayList. Object[] is just an array. Then you can use the toArray method to get the data back out.


[ February 21, 2006: Message edited by: steve souza ]
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic