// Searching an Array import java.applet.*; import java.awt.*; import java.awt.event.*; public class Array1 extends Applet implements ActionListener { // The array of integers between 0 and 9. private int array[] = { 3, 5, 6, 2, 3, 0, 8, 3, 6, 2 }; private Label title = new Label("Array Search"); // The search result. private Label result = new Label("No search yet"); // What to search for. private Label searchPrompt = new Label("Search for"); private TextField search = new TextField(20); private Button searchB = new Button("Search"); public void init() { Panel p = new Panel(new GridLayout(1,2)); setLayout(new GridLayout(4,1)); add(title); p.add(searchPrompt); p.add(search); add(p); add(result); add(searchB); searchB.addActionListener(this); setSize(275,100); } // Once we have the value to search for, search for them. public void actionPerformed(ActionEvent e) { int i; int target = Integer.parseInt(search.getText()); // The value to search for // Sequentially compare the target to each element in the array for (i = 0; i < array.length; ++i) if (array[i] == target) // Got a match. Return. { result.setText("Found at index " + i); return; } // If we get here, we didn't get a match. result.setText("Not found"); } }