Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Similar presentations


Presentation on theme: "Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value."— Presentation transcript:

1 Array Processing - 2

2 Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

3 Swapping two elements in an array. Often have to swap the values in two cells in an array. The following code attempts to swap the third and fourth element in the array (remember arrays are 0-based). IT DOESN’T WORK properly. Why? What are the resulting values in the array? int[] anArray = { 24, 36, 64, 48 }; anArray[2] = anArray[3]; anArray[3] = anArray[2];

4 Demo – Swap two array elements. Demo ArraySwap.java

5 Linear Search for a Value. Commonly need to search arrays to see if they contain a value. Technique involves : Iterate from first through last element of the array. For each iteration, compare value at current index with searchValue. If it is equal you can stop your search otherwise continue. Stop searching at the end of the array.

6 Demo: Linear Search public class LinearSearch1 { public static void main() { boolean found = false; int searchValue = 36; int[] anArray = { 24, 36, 64, 48 }; for( int i = 0 ; i < anArray.length && found != true ; i++ ){ if( anArray[i] == searchValue ){ found = true; } System.out.println(“Our search for " + searchValue + “ returned “ + found ); }

7 Finding a high value. Commonly need to find the largest or smallest element in an array. For a high value search: Create a simple variable, highValue, to store our highest value. Set it to the first element of the array. Iterate from second through last element of the array. For each iteration, compare value at current index with stored highValue. If it is greater it is the new high value, so reset highValue.

8 Demo: Find High Value public class HighValueSearch { public static void main() { int highValue = 0; int[] anArray = { 24, 36, 64, 48 }; highValue = anArray[0]; for( int i = 1 ; i < anArray.length; i++ ){ if( anArray[i] > highValue ){ highValue = anArray[i]; } System.out.println("High Value is " + highValue ); }

9 Your Turn!

10 Task:SimpleStats.java We want to write some code to generate simple statistics from our temperature array. Declare and initialize an array with the values 21.2, 22.0, 24.5, 27.2, 26.4. Write the code to detect the maximum value, the minimum value, the range (highest value minus lowest value) and the average temperature.

11 Passing and Returning Arrays. You will often want to pass an array to method or return an array from a method.

12 Array Parameters. you can use arrays as parameters. public static void printArray(double[] numbers) { for( double number: numbers ){ System.out.println( number); }....main double[] anArray = { 1.0,2.0,3.0 }; printArray( anArray );...end main.

13 Array Parameters. you can use arrays as parameters. public static int[] getMonthLengths() { int[] monthLengths = {31,28,31,30,31,30,31,31,30,31,30,31}; return monthLengths; }....main int[] anArray = getMonthLengths();...end main.

14 Your Turn Your previous in-class assignment was supposed to generate simple statistics (min,max,range,and mean). Now refactor this code this code so that the stats are calculate in a getStats method. getStats receives an array of double and returns an array of stats representing the min [0],max [1], range [2] and mean [3]. Use the method by calling it from main() and printing the output. public static double[] getStats( double[] temp );


Download ppt "Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value."

Similar presentations


Ads by Google