wood burning stoves*
The moose likes Beginning Java and the fly likes Dynamically specifying the size of an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamically specifying the size of an Array" Watch "Dynamically specifying the size of an Array" New topic
Author

Dynamically specifying the size of an Array

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
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.


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
use java.util.Vector. It is automatically resizable.
Thank you,
Maneesh


Sun Certified Java Programmer 2 (1.4)<br />Sun Certified Web Component Developer
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by maneesh:
Sun Certified Java Programmer 2 (1.4)

Then why are you proposing Vector instead of ArrayList??? :roll:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
because Vectors are threadsafe and a slightly older concept while arraylists are not thread safe
Thank you,
Maneesh
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
This article here may be helpful too.
Thank you,
Maneesh
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
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

Joined: Feb 27, 2002
Posts: 8919

Vector methods are synchronized so they incur more performance cost.


Groovy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically specifying the size of an Array