} } public int linearSearch( int array2[], int key )//是linearSearch { for( int counter = 0; counter < array2.length; counter++) { if( array2[counter] == key ) { return counter; } }
return -1; }//是linearSearch public void actionPerformed( ActionEvent actionEvent ) { String searchKey = actionEvent.getActionCommand(); int element = linearSearch( array, Integer.parseInt( searchKey )); if( element != -1 ) rezultField.setText( "Found value in element " + element ); else rezultField.setText( "value not found" ); } }
import javax.swing.*;
import java.awt.event.*;public class LinerSearch extends JApplet implements ActionListener
{
JLabel enterLabel, rezultLabel;
JTextField enterField, rezultField;
int array[]; public void init()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() ); enterLabel = new JLabel( "Enter integer searchkey" );
container.add( enterLabel ); enterField = new JTextField( 10 );
container.add( enterField );
enterField.addActionListener( this ); rezultLabel = new JLabel( "rezult" );
container.add( rezultLabel ); rezultField = new JTextField();
rezultField.setEditable( false );
container.add( rezultField ); array =new int[100]; for(int counter = 0; counter < array.length; counter++)
{
array[counter] = counter * 2;
}
} public int linearSearch( int array2[], int key )//是linearSearch
{
for( int counter = 0; counter < array2.length; counter++)
{
if( array2[counter] == key )
{
return counter;
}
}
return -1; }//是linearSearch public void actionPerformed( ActionEvent actionEvent )
{
String searchKey = actionEvent.getActionCommand(); int element = linearSearch( array, Integer.parseInt( searchKey )); if( element != -1 )
rezultField.setText( "Found value in element " + element );
else
rezultField.setText( "value not found" );
}
}