Arrays
In Chapter 7 (Section 7.1 to 7.9), we study how to use a data structure called array to organize and process data items.
Introduction to Array
Creating Array Objects
Array Index Out Of Bounds Exception
Array Processing
Sequential Search Algorithm
Enhanced for Loop
Two Dimensional Array
Command Line Arguments
Variable Length Arguments
Array Of Objects
Introduction to Array
An array is a data structure containing a number of data elements.All elements in an array have the same type.
It is a static data structure. Once created, the size of the array is fixed and cannot be changed.
Additional work must be done to "expand" the size of the array.
- Create a larger new array
- Copy the elements from the current array to the new array
- Rename the new array to the current array
- Copy the elements from the current array to the new array
- Rename the new array to the current array
We can access an element in the individual cell by its position (index) within the array.
The index of array cell starts from 0.
To access a particular element of an array, we write the array name followed by the index in square brackets (indexing).
Creating Array Objects
int [ ] myArray = new int[8]; //array of 8 ints with default value 0
Once the array is created, each cell is populated with the default value.
We can write code to change the values in array cells.
myArray[0] = 29;
myArray[1] = 9;
myArray[2] = 12;
myArray[3] = 8;
myArray[4] = 18;
myArray[5] = 10;
myArray[6] = 22;
myArray[7] = 20;
myArray[1] = 9;
myArray[2] = 12;
myArray[3] = 8;
myArray[4] = 18;
myArray[5] = 10;
myArray[6] = 22;
myArray[7] = 20;
Now the array elements are changed.
We can write code to access array elements using index.
System.out.println( myArray[0] + myArray[5] ); //39 is printed.
For a large array, we need to use a loop to process it.
for(int i = 0; i < myArray.length; i++) { //myArray.length is the array size.
System.out.print( myArray[i] + " ");
}
We can create an array of other data types such as double, String, boolean, and etc.
String [ ] nameArray = new String[100];
double [ ] priceArray = new double[10];
double [ ] priceArray = new double[10];
We can also create an array with hard-coded values.
int[ ] intArray = {3, 8, 31, 10, 21, 9}; //array with six cells
Array Index Out Of Bounds Exception
If an out of bound index is used to access the array element, an exception will occur and the program will crash.
int [ ] myArray = new int[8]; //array of 8 cells, from index 0 to 7
System.out.println( myArray[8] ); //index 8 is out of bound
System.out.println( myArray[8] ); //index 8 is out of bound
Array Processing
Creating an array of 25 with default value 0 in each cell:
int [ ] myArray = new int[25];
Populating the array cells with random numbers:
public static void populateArray( int [ ] myArray ) {
Random r = new Random(); //create a Random object
for(int i = 0; i < myArray.length; i++) {
for(int i = 0; i < myArray.length; i++) {
myArray[i] = r.nextInt(500); //0 - 499
}
}
Printing the array cells:
public static void printArray( int [ ] myArray ) {
for(int i = 0; i < myArray.length; i++) {
System.out.print( myArray[i] + " ");
}
}
Adding all the elements in the array cells:
public static int sumArray( int [ ] myArray ) {
int sum = 0;
for(int i = 0; i < myArray.length; i++) {
for(int i = 0; i < myArray.length; i++) {
sum += myArray[i];
}
return sum;
}
return sum;
Finding the index of the smallest element in the array:
public static int indexOfSmallest( int [ ] myArray ) {
int minIndex = 0; //assume the smallest is at index 0
for(int i = 1; i < myArray.length; i++) {
for(int i = 1; i < myArray.length; i++) {
if( myArray[i] < myArray[minIndex] ) { //compare the element at i with the element at minIndex
minIndex = i;
}
}
return minIndex;
}
return minIndex;
Sequential Search Algorithm
The sequential search algorithm is to locate a specific element in a collection of data such as array.
It uses a loop to sequentially visit each array cell and
compare each element with the searched value.
If the searched value is found, the process stops immediately and the index is returned.
If the searched value is not found after visiting all the array cells, -1 is returned.
If the searched value is found, the process stops immediately and the index is returned.
If the searched value is not found after visiting all the array cells, -1 is returned.
public static int sequentialSearch( int [ ] myArray, int key ) {
for(int i = 0; i < myArray.length; i++) {
if( key == myArray[i] ) { //compare the searched key with the array element at index i
return i;
}
}
return -1;
}
return -1;
Enhanced for Loop
It simplifies array processing.It always goes through all elements in the array.
int[ ] myArray = {12, 3, 6, 9, 5, 8, 7};
for(int value : myArray) {
for(int value : myArray) {
System.out.println(value);
}
Two Dimensional Array
A two-dimensional array is an array of arrays.It represents a table of data with rows and columns.
Creating a two-dimensional array requires two sets of brackets with the row index and column index.
double [ ][ ] tempTable = new double[3][4];
The first index is the number of rows.
The second index is the number of columns.
To process a two-dimensional array, it needs a nested loop.The second index is the number of columns.
The outer loop goes through rows while the inner loop steps through each column for each row.
tempTable.length refers to the number of rows.
tempTable[row].length refers to the number of columns at a particular row.
tempTable[row].length refers to the number of columns at a particular row.
Command Line Arguments
public static void main(String [ ] args) {
}
String [ ] args is an array of String.
}
String [ ] args is an array of String.
We can provide arguments when the program is executed.
Output:
Variable Length Arguments
public static void foo(double ... numberList) {
}
The length of the parameter numberList varies.
The caller can pass any number of arguments including an array of data items.
foo(); //no argument
foo( 5.6 ); //one argument
foo( 4.5, 5.6, 1.2, 23, 3.6 ); //five arguments
foo( myArray ); //an array of arguments
foo( 5.6 ); //one argument
foo( 4.5, 5.6, 1.2, 23, 3.6 ); //five arguments
foo( myArray ); //an array of arguments
Array Of Objects
Let's use the class City to demonstrate the concept.The City class has name and population as data fields. The class definition is as follows.
In the client program, let's create an array of City to store the data for 5 City objects.
City[ ] cityArray = new City[5];
Populate the array with user input:
Calculate the total population:
Determine the city with the highest population: