• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about expanding array

 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • 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?
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!