![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Notice that the length of the value array is at least doubled. It is not nearly expanded to the minimum necessary length. Although you dont want to waste space unnecessarily, its even more important not to waste CPU cycles by growing the array every time you have to add a character to it. You will see this scheme for growing arrays again very shortly. The java.util.Vector class does almost exactly the same thing.
The value array is also expanded when an item is inserted into the middle of a StringBuffer with one of the insert() methods. However, the value array will not be expanded to provide additional space if you try to insert an item past the end of the StringBuffer. If you try, a StringIndexOutOf BoundsException will be thrown. There are a few other useful methods in the StringBuffer class. The charAt(int i) method returns the ith char in the StringBuffer. This is easy to do because you can just return the ith char in the value array. The setCharAt(int i, char c) method changes the character at index i in the StringBuffer to the char c. This differs from the insert(int i, char c)method because it actually replaces the character at i rather than shifting it and all following characters to the right. A StringIndexOutOfBounds Exception is thrown if i equals or exceeds the length of the string. The length() method returns an int giving the number of characters currently present in the StringBuffer. This is generally not the same as the length of the value array. That number is called the StringBuffers capacity and is accessed with the capacity() method. The ensureCapacity() method already discussed changes a StringBuffers capacity. This is not the same as a StringBuffers length. The capacity of a StringBuffer is the number of characters that can be stored in it without taking time to expand the internal value array. The length of a StringBuffer is the number of characters currently stored in the internal value array. To change a StringBuffers length, invoke its setLength(int i) method. This does one of two things: if i is less than the current length of the StringBuffer, then the StringBuffer is truncated to the length i; however, if i is greater than the current length of the StringBuffer, then it is expanded with null characters to the requested length. The reverse() method reverses the characters in the StringBuffer in place. For example, if sb contains the string dam, after calling sb.reverse() it will contain the string mad. The getChars() method copies characters from the StringBuffer into an array. Its signature is public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) A substring of characters from the StringBuffer is copied into the char array dst. The substring to be copied is delineated on the left by the index srcBegin and on the right by srcEnd-1. The characters of this substring are copied into the subsection of the char array dst beginning at dstBegin. A StringIndexOutOfBoundsException is thrown if either srcBegin or srcEnd is less than zero or greater than or equal to the length of the string. An ArrayIndexOutOfBoundsException is thrown if dstBegin is less than zero or greater than or equal to the length of the array or if the substring exceeds the bounds of the array. Finally, like most other classes, StringBuffer contains a toString() method. This method is often invoked as the last step in a long sequence of string operations. java.util Data StructuresThe java.util class includes several abstract data structures to hold collections of objects. On one level, the programmer is supposed to be shielded from the internal workings of these classes. The whole point of a class library and abstract container classes is to shield the programmer from low-level details. However, there are performance issues that can be addressed only by learning how a class is implemented. For example, if the Vector class is implemented as a linked list, then insertions into the list will be very fast. However, finding a particular element in the list will be rather slow. On the other hand, if the Vector class is really an array, then insertions into the middle of the list may be quite slow but retrieving an element from the list can be quite fast. If youre writing an applet or application for public distribution, you may not wish to rely on what I say here about the internals of these classes. Although these data structures are implemented similarly on all Java implementations to which I have access, it is possible (though unlikely) that the implementation details will change in the future. If the performance of your program depends on a specific implementation of a class, then you may wish to copy the source code for the class, change its name, and recompile it. Then you would use your modified class instead of the original. Of course, this will increase the size of the program you have to distribute. This could be important for an applet, but is unlikely to be significant for an application. As with many other things, you must make the tradeoffs that are appropriate for your situation. You have to decide whats more important to you, guaranteed performance characteristics or smaller download size.
|
![]() |
|