Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Dynamically specifying the size of an Array

 
Ranch Hand
Posts: 524
  • 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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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: 8943
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector methods are synchronized so they incur more performance cost.
 
You didn't tell me he was so big. Unlike this 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
    Bookmark Topic Watch Topic
  • New Topic