packagesevenpoint;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassLinearSearchextendsJAppletimplementsActionListener{JLabelenterLabel,resultLabel;J...
package sevenpoint;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LinearSearch extends JApplet implements ActionListener {
JLabel enterLabel, resultLabel;
JTextField enterField, resultField;
int array[];
public void init() {
Container container = getContentPane();
container.setLayout( new FlowLayout() );
enterLabel = new JLabel( "Enter integer search key" );
container.add( enterLabel );
enterField = new JTextField( 10 );
container.add( enterField );
enterField.addActionListener( this );
resultLabel = new JLabel( "Result" );
container.add( resultLabel );
resultField = new JTextField( 15 );
resultField.setEditable( false );
container.add( resultField );
array = new int[ 100 ];
for ( int counter = 0; counter < array.length; counter++ )
array[ counter ] = 2 * counter;
}
public int linearSearch( int array2[], int key ){
for ( int counter = 0; counter < array2.length; counter++ )
if ( array[ counter ] == key )
return counter;
return -1;
}
public void actionPerformed( ActionEvent actionEvent ) {
String searchkey = actionEvent.getActionCommand();
int element = linearSearch( array, Integer.parseInt( searchkey ) );
if ( element != -1 )
resultField.setText( "Found value in element " + element );
else
resultField.setText( "Value not found" );
}
}
那个"key"是怎么出来的,没看懂这个程序。
展开