Presentation is loading. Please wait.

Presentation is loading. Please wait.

4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.

Similar presentations


Presentation on theme: "4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day."— Presentation transcript:

1 4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day

2 BinarySearch.java pt.1 //fig. 7.13: BinarySearch.java: binary search of an array import java.awt.*; import java.awt.event.*; import java.text.*; import javax.swing.*; public class BinarySearch extends JApplet implements ActionListener { JLabel enterLabel, resultLabel; JTextField enterField, resultField; JTextArea output; int array[]; String display = "";

3 BinarySearch.java pt.2 public void init () { Container container = getContentPane(); container.setLayout ( new FlowLayout() ); enterLabel = new JLabel ( "Enter key" ); container.add( enterLabel ); enterField = new JTextField( 5 ); enterField.addActionListener( this ); container.add( enterField ); resultLabel = new JLabel ( "Result" ); container.add( resultLabel );

4 BinarySearch.java pt.3 resultField = new JTextField( 22 ); resultField.setEditable( false ); container.add( resultField ); output = new JTextArea( 6, 60 ); output.setFont ( new Font( “Monospaced", Font.PLAIN, 12 ) ); container.add( output ); array = new int[ 15 ]; for ( int count = 0; count < array.length; count++ ) array[ count ] = 2 * count; }

5 BinarySearch.java pt.4 public void actionPerformed ( ActionEvent e ) { String searchKey = e.getActionCommand(); //initialize display string for new search display = "Portions of array searched\n"; //perform the binary search int element = binarySearch ( array, Integer.parseInt ( searchKey ) ); output.setText( display ); if ( element != -1 ) result.setText( "Found it in slot " + element ); else result.setText( "Value not found" ); }

6 BinarySearch.java pt.5 //binary search method public int binarySearch ( int array2[], int key ) { int low = 0; //low subscript int high = array.length - 1;//high subscript int middle; //middle subscript while ( low <= high ) { middle = ( low + high ) / 2; //The next line displays the part of the array still //available for searching through for the key. buildOutput ( array2, low, middle, high );

7 BinarySearch.java pt.6 if ( key == array[middle] ) //a match return middle; else if ( key < array[middle] ) high = middle - 1; //search lower array else low = middle + 1; } return -1; //searchKey not found }

8 BinarySearch.java pt.7 //build 1 row of output w/ part of the array being processed. void buildOutput ( int array3[], int low, int mid, int high ) { DecimalFormat twoDigits = new DecimalFormat ( "00" ); for ( int counter = 0; counter < a.length; counter ++ ) { if (counter high ) display += " "; else if (counter == mid ) //mark middle element in output display += twoDigits.format ( array3 [counter ] ) + "* "; else display += twoDigits.format ( array3 [counter ] ) + " "; } display += "\n"; }

9 Multiple-Subscripted Arrays Used to represent a table of values, not just a row. By convention, the 1st number = row number, and the 2nd number = column number. EX: int sample[] [] = new int [ 4 ] [ 2 ]; might have values like this: col 0col 1 row 031 row 120 row 268 row 375

10 Multiple-Subscripted Arrays Another way to remember: “first the row, then the position” int sample [] [] = { {3,1},{2,0},{6,8},{7,5} }; col 0col 1 row 031 row 120 row 268 row 375

11 Multiple-Subscripted Arrays EX: int sample[] [] = new int [ 4 ] [ 2 ]; sample [3][0] = 7 sample [1][1] = 0 sample [0][1] = ? sample [2][0] = ? col 0col 1 row 031 row 120 row 268 row 375

12 Multiple-Subscripted Arrays But how do you use them? Think of them as arrays of arrays (lists of lists). for ( int i = 0; i < id.length; i++ ) { for ( int j = 0; j < id[i].length ; j++ ){ g.drawString(“ “ + (id[ i ][ j ]),x,y); } see StringArray.java

13 Program of the Day pg. 347 DoubleArray.java


Download ppt "4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day."

Similar presentations


Ads by Google