Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Alter array size

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Is there any way by which i can increase my array size in Java?
Vector and such involve too much of over head...i wld like to use a String array itself.
regards
Menon
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If u want to increase the size of array at run time....then vector is the only solution.......

if u find diff using collection classes feel free to ask.

Ashish
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use System.arraycopy() to copy your array to the new one.
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arundhathi Menon:
Hi!
Is there any way by which i can increase my array size in Java?
Vector and such involve too much of over head...i wld like to use a String array itself.
regards
Menon



Possible to increase array size at runtime BUT you must have algorithm LIKE Vector (Dynamic Array)

Example code for Dynamic Array at runtime


Method in Vector's Algorithm ...

 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arundhathi Menon:
Vector and such involve too much of over head...



What kind of overhead? If you want to trade your current overhead with processing overhead in the code that Somkiat has just given, then go ahead with arraycopy method... but I suggest you to keep on using the vector... I do believe most would suggest you so...
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Array should be more *primitive* than Vector, and thus, it is a bit less overhead generated.

Just consider you can directly access the array with position i, but you need to access the same object in Vector through a method call (elementAt(i).

Nick
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
Array should be more *primitive* than Vector, and thus, it is a bit less overhead generated.

Just consider you can directly access the array with position i, but you need to access the same object in Vector through a method call (elementAt(i).



You are right, Nick... but imagine if we have to call arraycopy everytime the array got maximum space and copy to a larger array... The processing overhead would be critical as well in this situation..
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are right, Nick... but imagine if we have to call arraycopy everytime the array got maximum space and copy to a larger array... The processing overhead would be critical as well in this situation



I believe that System.arraycopy is a native method, it works faster. :roll:
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:


I believe that System.arraycopy is a native method, it works faster. :roll:



What if the arraycopy has to call many times to copy up all the items into a larger copy... But if we can predict the time that will reach the maximum elements in the array, we might have to apply some kind of algorithm in increasing the size of a larger array... Otherwise, I believe call a method several time consume processing power as well...
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Performance of ArrayList
The performance of Java's List


Performance of Copy an array
http://www.javapractices.com/Topic3.cjp
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic