• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Array Resizing

 
kris reddy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class with some data members in it. Data for the objects comes frm a XML file. I have to parse out the xml file and create objects. Before reading the XML file i dunno as to how many objects i need to create. I wud like to know a way where I can dynamically increase the size of the object array as I read through the XMl file.
Thanks in advance,
Kris.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once created, the length of arrays in Java cannot be changed. You may want to consider using a collection. Take a look at the four part series of articles on Java collections by Thomas Paul. They can be found in the past four JavaRanch newsletters. And take a look at the coverage of Java collections in Sun's Java Tutorial (their tutorial site is down at the moment, otherwise I'd give you a link).
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kris,
Use a java.util.ArrayList. You can do something like this:

The staticArray will contain all of your XML objects in the correct order.
Hope this helps,
Michael Morris
 
kris reddy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using a ArrayList, does the ArrayList dynamically allocate some memory for the object being stored or does it reference the address of the object location. I would like to know what happens internally.
Chill,
kris
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're concerned about performance, take a look at the ensureCapacity() method and the one-argument constructor, which takes an "initialCapacity" argument.
 
kris reddy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well my question is about the type of storage in an ArrayList. I understand that ensureCapacity operation dynamically reduces the amount of incremental reallocation.
Take it Ezy Ron,
kris
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Object array is used internally.
You can look at the source code for the java.util.ArrayList and much of the J2SE. Perhaps your installation of the JDK has a file called src.jar or src.zip in the root directory. Just unzip it and take a look.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic