If its too large space will be wasted which will be a problem especially when

If its too large space will be wasted which will be a

This preview shows page 7 - 9 out of 11 pages.

old one. If it’s too large, space will be wasted, which will be a problem especially when there are many instances of the collection type. Such implementations therefore provide constructors in which the client can set an initial capacity, from which the allocation size can be determined. ArrayList , for exam- ple, has the constructor public ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity. Parameters: initialCapacity - the initial capacity of the list. Throws: IllegalArgumentException - if the specified initial capacity is negative There are also methods that adjust the allocation: trimToSize , which sets the capacity so that the container is just large enough for the elements currently stored, and ensureCapacity , which increases capacity to some given amount. Using the capacity features is tricky. If you don’t have precise knowledge of how big your collections are for the particular application, you can run a profiler to find out. Note that this notion of capacity translates a behavioural problem into a performance problem – a very desirable tradeoff. In many old programs, there are fixed resource limits, and when they are reached, the program just fails. With the capacity approach, the program just slows down. It’s a good idea to design a program so that it works effi- ciently almost all the time, even if there’s a performance hit occasionally. If you study the implementation of the remove method in ArrayList , you’ll see this code: public Object remove(int index) { elementData[—size] = null; // Let gc do its work What’s going on? Isn’t garbage collection automatic? Herein lies a common mistake of many novice programmers. If you have an array in your representation, with a distinct instance variable holding an index to indicate which elements of the array are to be considered part of the abstract collection, it’s tempting to think that to remove ele- ments all you need to do is decrement this index. An analysis in terms of the abstrac- tion function will support this confusion: elements that fall above the index are, after 99
Image of page 7
all, not considered part of the abstract collection, and their values are irrelevant. There’s a snag, however. If you fail to assign null to the unused slots, the elements whose references sit in those slots will not be garbage collected, even if there are no other references to those elements elsewhere in the program. The garbage collector can’t read the abstraction function, so it doesn’t know that those elements are not real- ly reachable from the collection, even though they are reachable in the representation. If you forget to null out these slots, the performance of your program may suffer badly. 16.6 Copies, Conversions, Wrappers, etc All the concrete collection classes provide constructors that take collections as argu- ments. These allow you to copy collections, and to convert one collection type to another. For example, LinkedList has public LinkedList(Collection c)
Image of page 8
Image of page 9

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture