Java Reference
In-Depth Information
Coding the enlargeArray() Method
The method to enlarge the array by one, as the user enters new data, is
shown in Figure 7-28.
250
//method to enlarge an array by 1
251
public String [] enlargeArray ( String [] currentArray )
252
{
253
String [] newArray = new String [ currentArray.length + 1 ] ;
254
for ( int i = 0; i<currentArray.length; i++ )
255
newArray [ i ] = currentArray [ i ] ;
256
return newArray;
257
}
258
FIGURE 7-28
The method accepts the currentArray passed to it and returns a new String
array. The new array is constructed to be one record longer in line 253. The for()
loop beginning in line 254 populates the array with the old data. Once the return
is executed, the actionPerformed() method inserts the newest data at the end of
the array (lines 228 through 230 in Figure 7-26 on page 461).
The following step enters code for the enlargeArray() method.
To Code the enlargeArray() Method
1. Enter the code shown in Figure 7-28.
TextPad displays the code for the enlargeArray() method (Figure 7-29).
enlargeArray()
method
FIGURE 7-29
The methods needed to create the menu system — add components to the
content pane, insert new data into arrays, and respond to clicks in the interface
— now are complete. The next two sections in the chapter present the sort and
search algorithms.
Sorting an Array
Sorting an array is the process of arranging the array's elements in ascending
or descending order. Any time large amounts of data are maintained, the data
needs to be arranged in a particular order for simplified searching or for logical
Search WWH ::




Custom Search