• 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

Dynamically specifying the size of an Array

 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I just wanted know, is there a possible way to add more memory for a single dimension
array. I mean if you declare an array of 7 elements and say if you want to have 10 elements
how can I achieve this.
Thanks.
If there is a way is it ok to do it with other objects too. Say for instance if I have a
class named 'RItem'. Is it possible to add more elements to a declared array.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to declare a bigger array, then copy over the old elements (Using System.arraycopy(), usually.) You can use an ArrayList or a Vector as basically a wrapper around an array that deals with resizing automatically.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use java.util.Vector. It is automatically resizable.
Thank you,
Maneesh
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by maneesh:
Sun Certified Java Programmer 2 (1.4)


Then why are you proposing Vector instead of ArrayList??? :roll:
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because Vectors are threadsafe and a slightly older concept while arraylists are not thread safe
Thank you,
Maneesh
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article here may be helpful too.
Thank you,
Maneesh
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by maneesh subherwal:
This article here may be helpful too.


The article fails to mention that you can make an ArrayList threadsafe by using http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#synchronizedList
It also states "By using an array you can avoid synchronization" which seems to be total bogus to me...
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by maneesh subherwal:
because Vectors are threadsafe and a slightly older concept while arraylists are not thread safe
Thank you,
Maneesh


Vectors should never be used. An ArrayList is easily made threadsafe.
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Ilja and Thomas, for your responses.

Originally posted by Thomas Paul:

Vectors should never be used. An ArrayList is easily made threadsafe.


could you go more into detail about this concept. I was wondering why the two of you are so against the use of a vector. It would be helpful in understanding it a little more.
Thank you,
Maneesh
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vector methods are synchronized so they incur more performance cost.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic