System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə26/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   22   23   24   25   26   27   28   29   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

type array­name[ ] = { val1, val2, val3, ... , valN };
Here, the initial values are specified by val1 through valN. They are assigned in
sequence, left to right, in index order. Java automatically allocates an array large
enough to hold the initializers that you specify. There is no need to explicitly use the
new operator. For example, here is a better way to write the MinMax program:


Array boundaries are strictly enforced in Java; it is a run­time error to overrun or
underrun the end of an array. If you want to confirm this for yourself, try the following
program that purposely overruns an array:
As soon as i reaches 10, an ArrayIndexOutOfBoundsException is generated and
the program is terminated.
Try This 5­1
Sorting an Array
Because a one­dimensional array organizes data into an indexable linear list, it is the
perfect data structure for sorting. In this project you will learn a simple way to sort an
array. As you may know, there are a number of different sorting algorithms. There are
the quick sort, the shaker sort, and the shell sort, to name just three. However, the best
known, simplest, and easiest to understand is called the Bubble sort. Although the
Bubble sort is not very efficient—in fact, its performance is unacceptable for sorting


large arrays—it may be used effectively for sorting small arrays.
1. Create a file called Bubble.java.
2. The Bubble sort gets its name from the way it performs the sorting operation. It uses
the repeated comparison and, if necessary, exchange of adjacent elements in the array.
In this process, small values move toward one end and large ones toward the other end.
The process is conceptually similar to bubbles finding their own level in a tank of
water. The Bubble sort operates by making several passes through the array,
exchanging out­of­place elements when necessary. The number of passes required to
ensure that the array is sorted is equal to one less than the number of elements in the
array.
Here is the code that forms the core of the Bubble sort. The array being sorted is called
nums.
Notice that sort relies on two for loops. The inner loop checks adjacent elements in the
array, looking for out­of­order elements. When an out­of­order element pair is found,
the two elements are exchanged. With each pass, the smallest of the remaining
elements moves into its proper location. The outer loop causes this process to repeat
until the entire array has been sorted.
3. Here is the entire Bubble program:


The output from the program is shown here:


4. Although the Bubble sort is good for small arrays, it is not efficient when used on
larger ones. A much better general­purpose sorting algorithm is the Quicksort. The
Quicksort, however, relies on features of Java that you have not yet learned about.
MULTIDIMENSIONAL ARRAYS
Although the one­dimensional array is often the most commonly used array in
programming, multidimensional arrays (arrays of two or more dimensions) are
certainly not rare. In Java, a multidimensional array is an array of arrays.
Two-Dimensional Arrays
The simplest form of the multidimensional array is the two­dimensional array. A two­
dimensional array is, in essence, a list of one­dimensional arrays. To declare a two­
dimensional integer array table of size 10, 20 you would write
Pay careful attention to the declaration. Unlike some other computer languages, which
use commas to separate the array dimensions, Java places each dimension in its own
set of brackets. Similarly, to access point 3, 5 of array table, you would use table[3]
[5].
In the next example, a two­dimensional array is loaded with the numbers 1 through 12.


In this example, table[0][0] will have the value 1, table[0][1] the value 2, table[0]
[2] the value 3, and so on. The value of table[2][3] will be 12. Conceptually, the array
will look like that shown in 
Figure 5­1
.
Figure 5­1 Conceptual view of the table array by the TwoD program
Irregular Arrays
When you allocate memory for a multidimensional array, you need to specify only the
memory for the first (leftmost) dimension. You can allocate the remaining dimensions
separately. For example, the following code allocates memory for the first dimension of
table when it is declared. It allocates the second dimension manually.
Although there is no advantage to individually allocating the second dimension arrays
in this situation, there may be in others. For example, when you allocate dimensions
separately, you do not need to allocate the same number of elements for each index.
Since multidimensional arrays are implemented as arrays of arrays, the length of each
array is under your control. For example, assume you are writing a program that stores
the number of passengers that ride an airport shuttle. If the shuttle runs 10 times a day
during the week and twice a day on Saturday and Sunday, you could use the riders
array shown in the following program to store the information. Notice that the length of
the second dimension for the first five indices is 10 and the length of the second
dimension for the last two indices is 2.


The use of irregular (or ragged) multidimensional arrays does not, obviously, apply to
all cases. However, irregular arrays can be quite effective in some situations. For
example, if you need a very large two­dimensional array that is sparsely populated (that
is, one in which not all of the elements will be used), an irregular array might be a
perfect solution.
Arrays of Three or More Dimensions


Arrays of Three or More Dimensions
Java allows arrays with more than two dimensions. Here is the general form of a
multidimensional array declaration:

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   22   23   24   25   26   27   28   29   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə