|
-
October 17th, 2002, 03:43 PM
#6
Member
If I knew how many objects I was going to limit my program to use I would use an array. That way you don't have to cast the objects because they are already referenced as the proper class.
If I didn't know how many objects I was going to have to track, then I probably wouldn't define an array, simply because it can't grow as needed.
When you define the array you would have to do something like
AccountOject[] accountArray = new AccountObject[1000];
If objects are being created via user input then there wouldn't be a for loop, you would add them to the array as they are created, so if the last one added is number 1001 you will get a runtime exception unless you check the number of array elements. This solution isn't very scalable because you've set the limit and you will need to change code to add more elements.
By using a Vector collection object you can put a virtually unlimited number of AccountObject objects into it and grow the Vector as needed. For example:
import java.util.*;
Vector accountVector = new Vector(1000, 100);
Now you have a collection of objects that starts at 1000, but it grows by 100 when you add the 1001th record. The reason to grow it by a set number is that it takes resources to extend a Vector, so if you do it in acceptable increments you aren't increasing the size for each add. Another benefit is that you can easily search for and replace values in a Vector.
[Edit] The only catch to a Vector is that when you retrieve an object it is stored as type Object, so you have to cast it back to an AccountObject before you use it, or you could get an exception.
When it comes to scalability the Collections feature in Java is a great alternative to traditional arrays.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|