| Author |
Dynamic Array Resizing
|
kris reddy
Greenhorn
Joined: Nov 07, 2000
Posts: 19
|
|
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.
|
Kris Lee
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
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).
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
|
|
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
|
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
|
 |
kris reddy
Greenhorn
Joined: Nov 07, 2000
Posts: 19
|
|
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
Joined: Jun 06, 2002
Posts: 1056
|
|
|
If you're concerned about performance, take a look at the ensureCapacity() method and the one-argument constructor, which takes an "initialCapacity" argument.
|
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
|
 |
kris reddy
Greenhorn
Joined: Nov 07, 2000
Posts: 19
|
|
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
Joined: Dec 10, 2001
Posts: 7023
|
|
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.
|
 |
 |
|
|
subject: Dynamic Array Resizing
|
|
|