This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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).
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
Joined: Nov 07, 2000
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
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)
Joined: Nov 07, 2000
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
Joined: Dec 10, 2001
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.