File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically specifying the size of an Array

 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
maneesh subherwal
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use java.util.Vector. It is automatically resizable.
Thank you,
Maneesh
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article here may be helpful too.
Thank you,
Maneesh
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector methods are synchronized so they incur more performance cost.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic