Page 430 - Introduction to Programming with Java: A Problem Solving Approach
P. 430
396 Chapter 10 Arrays and ArrayLists
/***************************************************************
*
SortDriver.java
Dean & Dean
This exercises selection sort in class Sort.
***************************************************************/
*
*
*
public class SortDriver
{
public static void main(String[] args)
{
int[] studentIds = {3333, 1234, 2222, 1000};
Sort.sort(studentIds);
for (int i=0; i<studentIds.length; i++)
}
// end SortDriver
}
// end main
{
}
System.out.print(studentIds[i] + " ");
calling the sort method
Figure 10.16 Driver that exercises the sort method in Figure 10.15
Apago PDF Enhancer
Here’s skeleton code for how you might use the Arrays class’s sort method: import java.util.Arrays;
...
int[] studentIds = {...};
...
Arrays.sort(studentIds);
We recommend that you use this API method for heavy-duty sorting. It’s an overloaded method, so it also works for arrays of other types of primitive variables.
10.9 Two-Dimensional Arrays
Arrays are good for grouping related data together. Up to this point, we’ve grouped the data together using standard one-dimensional arrays. If the related data is organized in a table format, consider using a two- dimensional array. In this section, we describe two-dimensional arrays.
Two-Dimensional Array Syntax
Two-dimensional arrays use the same basic syntax as one-dimensional arrays except for a second pair of square brackets ([]). Each pair of square brackets contains one index. According to standard programming practice, the first index identifies the row and the second index identifies the column position within a row.