










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











solves the problem of storing a large number of values and manipulating them is a data structure designed to store a fixed-size sequential collection of elements of the same type, i.e., it is a collection of variables of the same type
creates a storage location for a Reference to an Array, i.e., creating a Reference Variable for an Array
double[ ] temperature; -- preferred notation double temperature[ ]; -- inherited from the C programming language
Specify the Array Size, i.e., Determine the Array Length Allocate Memory for the Array & Assign a Reference to that Memory Location
temperature = new double[24];
which allocates sufficient memory to store 24 different temperature readings
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
double[ ] temperature = new double[24];
Allocating Memory for an Array of doubles
Assigning a Reference to that Memory Location
The statement double[ ] temperature = new double[6]; creates an array
0 1 2 3 4 5 where each cell can be referenced by its index, e.g., temperature[0] = 54.7; which stores the value 54.7 in cell 0
0 1 2 3 4 5 and double x = temperature[0]; which retrieves the value 54.7 from cell 0 and places it in the variable x.
is determined when the array is created, i.e., when memory is allocated size of an existing array, e.g., temperature, can be determined by temperature.length which for the above example, returns the value 6 once the array is allocated, the length cannot be modified
temperature[0] = temperature[0] + 5.9; temperature[0] 54.7 + 5.
double[ ] temperature = {54.7, 453.9, 657.7, 342.2, 564.9, 769.8};
index values
Incrementing variable values
for( <data-type> element: temperature) System.out.println(element);
Remember that double[ ] temperature creates a Reference Variable, i.e., creates storage space for a reference to an array, but does not allocate storage space for an array.
The statement double[ ] temperature1 = new double[24]; creates a reference variable for an array, allocates memory for an array& assigns the reference to that array.
The reference variable temperature1 can be visualized as either a handle for the array or a pointer to the array.
Thus the statements double[ ] temperature2; temperature2 = temperature1; creates a new reference variable temperature2 and making it refer to the same array as the reference variable temperature1.
a. Copying Arrays Using a Loop Statement
double[ ] temperature1 = {54.7, 453.9, 657.7, 342.2, 564.9, 769.8};
double[ ] temperature2 = new double[temperature1.length];
for( i=0; i < temperature1.length; i++) temperature2[ i ] = temperature1[ i ];
element must be the same type as the elements in temperature For-Each Loops cannot be used to traverse the array in a different order nor for modifying the values of the array
Array of Temperature Values
temperature
Temperature
b. Copying an Array using the Static arraycopy Method
java.lang.System contains the method arraycopy arraycopy violates the java naming convention! syntax arraycopy(sourceArray, src_pos, targetArray, tar_pos, length);
double[ ] temperature1 = {54.7, 453.9, 657.7, 342.2, 564.9, 769.8}; double[ ] temperature2 = new double[temperature1.length]; System.arraycopy(temperature1, 0, temperature2, 0, temperature1.length);
c. Copying an Array using the Clone Method (to be discussed in Chapter 10)
public static void printArray( int [ ] array) { for( int i = 0; i < array.length; i++ ) System.out.print( array [i] + “ “); }
printArray( new int [ ] { 3, 1, 2, 6, 2 });
Java uses pass-by-value to pass arguments to a method
For an argument of a primitive type, the arguments value is passed. For an argument of an array type, the value of the argument is a reference to an array; the value of the reference variable is passed. The effect is that arrays are passed by reference, i.e., the method has access to the values stored in the array.
source starting position target starting position number of elements to be copied
Invocation of printArray method
Definition of printArray method
Anonymous array, i.e., there is no explicit reference variable holding the array; hence the array does not exist outside of the parameter list of the printArray method
public static void swap(int n1, n2) { int temp = n1; n1 = n2; n2 = temp; }
public static void swapFirstTwoInArray(int [ ] array) { int temp = array[0]; array[0] = array[1]; array[1] = temp; }
Invocation Statements
int [ ] a = {1, 2}; swap( a[0], a[1]); swap( 1, 2 ); thus after swap a == {1, 2} swapFirstTwoInArray(a); after swap a == {2, 1}
public static int [ ] reverse(int [ ] list) { int [ ] result = new int [ list.length ]; for( int i = 0, j = result.length – 1; i < list.length; i++, j - - ) result [ j ] = list [ i ]; return result; }
int [ ] list1 = {1, 2, 3, 4, 5, 6}; int [ ] list2 = reverse(list1); list2 == {6, 5, 4, 3, 2, 1}
a[0]: 1 a[1]: 2
Space -- main method int [ ] a
Space – swapFirstTwoInArray method int [ ] array
Space -- main method int [ ] a
Space – swap method n2: 2 n1: 1
There is no way for the swap method to send the values back to the array a, i.e., pass by value
Listing 6.
Use the methods
public static char getRandomCharacter(char ch1, char ch2) { return (char) (ch1 + Math.random( ) * (ch2 ch1 + 1)); }
public static char getRandomLowerCaseLetter( ) { return = getRandomCharacter(„a‟, „z‟); }
to build the array chars (see Liang pages 193-194 for details)
Use the method
public static int [ ] countLetters(char [ ] chars) { int [ ] counts = new int[26]; for ( int i = 0; i < chars.length; i++ ) counts[ chars[ i ] – „a‟ ]++; return counts; }
to record the count of each lower case letter in an array
Searches an array by comparing the value of a search item with each element in the array one-by-one; e.g., for the array a, below, the search for the value 564.9 would search, in order, cells 0, 1, 2, 3, and then 4 after which the search would terminate.
a
0 1 2 3 4 5
A linear search method can be written is as follows:
public class linearSearch( int [ ] list, int key ) // key is the search item { for ( int = 0; i < list.length; i++ ) if (key == list[ i ]) { return i; } return -1; }
It returns the index of the desired item as specified in the parameter key if such an item is found in the list, otherwise it returns -1 to signal failure to find such an item.
The execution time of a linear search increases linearly as n increases, hence it is said to be of the order n, i.e., O(n). On average it takes n/2 tries to find the item if it is in the array and n tries if it is not in the array. The linear search algorithm is usually avoided for searches in arrays with large n values.
In order to use this algorithm, the array must be sorted.
Assume that it has been sorted in an ascending order, e.g., such as a telephone book. Think of how you search a phonebook; if you are searching for a last name that begins with the l etter „K‟, you generally open the book in the middle and determine whether the letter „K‟ falls in the first half or the second half of the book. In some areas, it will fall in the first and in others it will fall in the second. In either case, you then ig nore, i.e., “throw away” the other half of the phonebook as being irrelevant to the current search. You then repeat the process with the relevant section of the phonebook until you locate the desired name or determine that no such person is listed in that phonebook. This is a binary search.
In the first pass, you only need to search ½ of the original list; in the second pass you only search ½ of ½ of the original list; etc., etc., ….
Assume n is a power of 2, i.e., n == 2m^ for some m. After the first pass there n/ elements left to search, after the second pass there are (n/2)/2 elements left to search and after the kth^ search there are n/2k^ elements to search.
When n == log 2 n, only one more element is left in the array and only one more search is necessary to determine whether the desired item is in the array. Hence, in the worst case, binary search requires at most log 2 n + 1 comparisons.
Remember that the linear search on an array of length n required n comparisons in the worst case.
Worst case search on a list of 1024 elements Linear Search 1023 comparisons Binary Search 11 comparisons
Defining a Binary Search Algorithm
that is currently being searched. Initially low = 0 and high = list.length-1;
being searched.
Hence mid = (low + high)/2;
The binary search algorithm given below, returns the index of the desired item, if such an item is contained in the array the value -1*(insertion point -1), if the item is not in the array
public class BinarySearch { public static int binarySearch(int [ ] list, int key) { int low = 0; int high = list.length - 1; while ( high >= low) { int mid = (low + high)/2; if ( key < list[mid]) high = mid -1; else if ( key == list[mid]) return mid; else low = mid + 1; } Return – low -1; // in this case we have high < low } } Homework: page 198 trace the algorithm as suggested in the text!!
remember that the indexes are all integers so that the computation of mid uses integer division, e,g, mid = ( 15 + 42 )/2 yields 28.
9300% improvement
where the insertion point is the index of the point at which the item should be inserted into the array to maintain the sorted order of the array
Moving on cell at a time across the array, place the selected value in its correct location, shifting the values of all the other cells to make the necessary room, e.g., forming a military line based on the decreasing/increasing height of the solders
Example
2 9 5 4 8 1 6 // no move
public class Insertionsort { public static void insertionSort(double [ ] list) { for ( int i = 1; i < list.length; i++ ) { double currentElement = list[ i ]; int k; for ( k = i -1; k >= 0 && list[ k ] > currentElement; k --) list[ k + 1 ] = list[ k ]; list[ k + 1 ] = currentElement; } } }
Homework: page 202 trace the algorithm as suggested
java.util.Arrays contains various static methods dealing with arrays overloaded for the primitive types java.util.Arrays.sort(numbers); // sorts the array numbers java.util.Arrays.sort(numbers, 4, 17); // sorts the portion of the array between numbers[4] and numbers[16], inclusive, i.e., [4 , 17).
numbers
0 4... 16 17
java.util.Arrays.binarySearch(<array_name, search_target>); e.g., java.util.Arrays.binarySearch(numbers, 657.7);
java.util.Arrays.equals(list1, list2); e.g., if list1, list2 and list3 are defined as int [ ] list1 = { 1, 2, 3, 5 }; int [ ] list2 = { 1, 2, 3, 5 }; int [ ] list3 = { 1, 2, 3, 4 }; then java.util.Arrays.equals(list1, list2) returns TRUE and java.util.Arrays.equals(list1, list3) returns FALSE
java.util.Arrays.fill(list1, 0) list1 contains { 0, 0, 0, 0 } and java.util.Arrays.fill(list2, 1, 2, 0) list2 contains { 1, 0, 0, 5 }
int [ ] [ ] messyArray = { {1, 2, 3}, (4, 5, 6, 7, 8, 9 }, { 0 }, { 7, 6, 35, 29, 190, -19, 57, 209, 41 }, { 987, 8796, 1982 } }; produces
int [ ] [ ] messyArrayB = new int [5][ ]; messyArrayB[0] = new int[3]; messyArrayB[1] = new int[6]; messyArrayB[2] = new int[1]; messyArrayB[3] = new int[9]; messyArrayB[4] = new int[3];
produces messyArrayB
int [ ] [ ] matrix = new int [5][10]; int total = 0;
for ( int row = 0; row < matrix.length; row++ ) { for ( column = 0; column < matrix[row].length; column++ ) { matrix[row][column] = (int)(Math.random( ) * 100); } }
for ( int row = 0; row < matrix.length; row++ ) { for ( column = 0; column < matrix[row].length; column++ ) { System.out.print(matrix[row][column] + “ “); } System.out.println( ); }
for ( int row = 0; row < matrix.length; row++ ) { for ( column = 0; column < matrix[row].length; column++ ) { total += matrix[row][column]; } }
for ( int column = 0; column < matrix[0].length; column++ ) { int total = 0; for ( row = 0; row < matrix.length; row++ ) total += matrix[row][column]; System.out.println(“Column: “ + column + “Total: “ + total ); }
for ( row = 0; row < matrix.length; row++ ) { int total = 0; for ( int column = 0; column < matrix[0].length; column++ ) total += matrix[row][column]; System.out.println(“ Row : “ + row + “Total: “ + total ); }
matrix.length == 5
matrix[row].length == 10 i.e., matrix[0].length == 10