1156 Java Utilities Package and Bit (Professional web hosting) Manipulation Chapter
Wednesday, April 25th, 20071156 Java Utilities Package and Bit Manipulation Chapter 20 Line 195 calls Vectormethod trimToSize to reduce the capacity of the Vector to the current number of elements in the Vector(i.e., the Vector s current size). Lines 213 214 use Vectormethods size and capacity to determine the number of elements currently in the Vectorand the number of elements that can be stored in the Vectorwithout allocating more memory, respectively. Line 231 calls Vectormethod elements to return an Enumeration that enables the program to iterate through the Vector s elements. An Enumeration provides two methods hasMoreElements and nextElement. In line 234, method has- MoreElements returns true if there are more elements in the Vector. In line 235, method nextElementreturns a reference to the next element in the Vector. If there are no more elements, method nextElementthrows a NoSuchElementException. For complete information on class Vectorand its other methods, see the online Java API documentation. 20.3 StackClass In Chapter 19, Data Structures, we learned how to build such fundamental data structures as linked lists, stacks, queues and trees. In a world of software reuse, instead of building data structures as we need them, often we can take advantage of existing data structures. In this section, we investigate class Stack in the Java utilities package (java.util). In Section 20.2, we discussed class Vector, which implements a dynamically resizable array. Class Stack extends class Vector to implement a stack data structure. As does Vector, class Stackstores references to Objects. To store primitive data types, use the appropriate type-wrapper class to create an object containing the primitive value (Boolean, Byte, Character, Short, Integer, Long, Float or Double). Figure 20.2 provides a GUI that enables the user to test each of the Stackmethods. // Fig. 20.2: StackTest.java // Testing the Stack class of the java.util package // Java core packages import java.awt.*; import java.awt.event.*; import java.util.*; // Java extension packages import javax.swing.*; public class StackTest extends JFrame { private JLabel statusLabel; private JTextField inputField; private Stack stack; // create GUI to manipulate a Stack public StackTest() { super( “Stacks” ); Fig. 20.2 Demonstrating class Stackof package java.util(part 1 of 5).
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services